Description Q: I learned how to weld circuit boards in the Electronic Technology Practice class. A circuit board consists of several components. We may call it a node and use it with numbers 1, 2, 3 .... Label. Each node of a circuit board is connected by several non-Intersecting wires. Any two nodes of the circuit board have only one path (path refers to the wire sequence connecting two components ). There is a special component on the circuit board called a "hair generator ". When the repeater works, an exciting current is generated and transmitted through a wire to each node connected to it. After receiving the excitation current, the intermediate node obtains the information and transmits the excitation current to the node connected to it and has not yet received the excitation current. In the end, intense current will reach some "termination nodes"-nodes that are no longer forwarded after receiving the excitation current. It takes time for the excitation current to spread over the wire. For each side e, the time required for the excitation current to pass through is te, the forwarding after the node receives the excitation current can be considered to be completed in an instant. This circuit board now requires each "terminated node" to receive an Excitation Circuit at the same time-that is, to maintain temporal synchronization. Because the current structure does not meet the requirements of temporal synchronization, you need to change the structure of the connection line. Currently, Xiao Q has a prop. Using this prop once can increase the time of the excitation current to pass through a connection wire by one unit. How many times does Xiao Q use at least to synchronize all the "termination nodes" temporal statements? The first line of input contains a positive integer N, indicating the number of nodes in the circuit board. The second line contains an integer (s), which is the number of the circuit breaker. Next the N-1 line, each line of three integers a, B, T. It indicates that the wire connects node A and Node B, and it takes t units of time for the excitation current to pass through the wire. Output only contains an integer v, which is the minimum number of items used by Q. Sample input311 2 11 3 3 sample output2 [data scale] for 40% of data, n ≤ 1000 for 100% of data, n ≤ 500000 for all data, te ≤ 1000000
After a subsequent traversal, Every time all the sub-trees are synchronized with the "slowest" node, all these costs can be accumulated.
But in windows, the stack can only pass 70 points.
#include <cstdio>#include <cstdlib>#include <algorithm>#include <string>typedef long long int64;const int maxN = 500010;const int64 INF = 0x3f3f3f3f3f3f3f3fLL;struct Edge{ int u, v; int64 d; Edge *next; Edge() {} Edge(int u, int v, int64 d, Edge *next): u(u), v(v), d(d), next(next) {}} *edge[maxN];bool marked[maxN]; int n, S; int64 dist[maxN], ans;void Dfs(int &u){ int64 Max = 0; dist[u] = 0; marked[u] = 1; for (Edge *p = edge[u]; p; p = p -> next) if (!marked[p -> v]) { Dfs(p -> v); Max = std::max(Max, p -> d + dist[p -> v]); } for (Edge *p = edge[u]; p; p = p -> next) if (!marked[p -> v]) { ans += Max - dist[p -> v] - p -> d; dist[p -> v] = Max - p -> d; } dist[u] = Max; marked[u] = 0; return;}inline int getint(){ int res = 0; char tmp; while (!isdigit(tmp = getchar())); do res = (res << 3) + (res << 1) + tmp - '0'; while (isdigit(tmp = getchar())); return res;}int main(){ freopen("synch.in", "r", stdin); freopen("synch.out", "w", stdout); n = getint(); S = getint(); for (int i = 1; i < n; ++i) { int u = getint(), v = getint(); int64 d = getint(); edge[u] = new Edge(u, v, d, edge[u]); edge[v] = new Edge(v, u, d, edge[v]); } Dfs(S); printf("%I64d\n", ans); return 0;}