Hdu 2196 Computer tree DP, hdu2196
Link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2196
Start with a computer, and then add a N-1 computer (N <= 10000 ). there are two integers, a and L, from line 1 to line N, indicating that the distance between the computer I and a is L.
The maximum distance between each computer and other computers is required;
Train of Thought: after creating the DFS, we can find the maximum distance from each vertex as the root node to the leaf node. I refer to the chain with this vertex as the root as the heavy chain;
In other cases, the maximum value of a parent node is updated through a parent node that is not on the heavy chain;
Note: When this point is modified, the values of the nodes in the subtree with the root node need to be modified; that is, recursion; only the first few vertices on the heavy chain of the entire tree do not need to be modified. vertices not on the heavy chain can be updated through the heavy chain. vertices on the heavy chain can be updated, if a node changes, the points under the heavy chain change accordingly;
Details: Use dp [u] [2] to specify the maximum length and maximum length of the root node. The maximum length of a heavy chain cannot be changed .. And assign values directly (max is not required );
# Include <iostream> # include <cstdio> # include <cstring> # include <string. h> # include <algorithm> # include <vector> # include <cmath> # include <stdlib. h> # include <time. h> # include <stack> # include <set> # include <map> # include <queue> using namespace std; # define rep0 (I, l, r) for (int I = (l); I <(r); I ++) # define rep1 (I, l, r) for (int I = (l ); I <= (r); I ++) # define rep_0 (I, r, l) for (int I = (r); I> (l); I --) # define rep_1 (I, r, l) for (I Nt I = (r); I >= (l); I --) # define MS0 (a) memset (a, 0, sizeof (a) # define MS1 () memset (a,-1, sizeof (a) # define MSi (a) memset (a, 0x3f, sizeof (a) # define inf 0x3f3f3f3f # define lson l, m, rt <1 # define rson m + 1, r, rt <1 | 1 typedef pair <int, int> PII; # define A first # define B second # define MK make_pairtypedef _ int64 ll; template <typename T> void read1 (T & m) {T x = 0, f = 1; char ch = getchar (); while (ch <'0' | ch> '9') {I F (ch = '-') f =-1; ch = getchar () ;}while (ch> = '0' & ch <= '9 ') {x = x * 10 + ch-'0'; ch = getchar ();} m = x * f;} template <typename T> void read2 (T &, T & B) {read1 (a); read1 (B);} template <typename T> void read3 (T & a, T & B, T & c) {read1 (a); read1 (B); read1 (c);} template <typename T> void out (T a) {if (a> 9) out (a/10); putchar (a % 10 + '0');} # define N 10010int dp [N] [3], ans [N]; int head [N], tot; struct Edge {int to, w, Next; Edge () {} Edge (int, Int w, int Next): to (to), w (w), Next (Next) {}} e [N <1]; inline void ins (int u, int v, int w) {e [++ tot] = Edge {v, w, head [u]}; head [u] = tot;} void dfs (int u, int pre) {for (int d = head [u]; d = e [d]. next) {int v = e [d]. to; if (v = pre) continue; dfs (v, u ); if (dp [u] [0] <= dp [v] [0] + e [d]. w) {dp [u] [1] = dp [u] [0]; dp [u] [0] = dp [v] [0] + e [d]. w;} else if (dp [v] [0] + e [d]. w> dp [u] [1]) dp [u] [1] = dp [v] [0] + e [d]. w ;}} Void solve (int u, int pre) {for (int d = head [u]; d = e [d]. next) {int v = e [d]. to, w = e [d]. w; if (v = pre) continue; // cout <u <"<v <" "<dp [u] [1] <" "<dp [u] [0] <<endl; if (dp [u] [1] =-1 | dp [u] [0]-w! = Dp [v] [0]) // The parent node has changed or is not a heavy chain dp [v] [0] = dp [u] [0] + w, dp [v] [1] =-1; else if (dp [u] [0]-w = dp [v] [0]) {if (dp [u] [1] + w> = dp [v] [0]) dp [v] [0] = dp [u] [1] + w, dp [v] [1] =-1; else dp [v] [1] = dp [u] [1] + w; // ** the value of the update count;} solve (v, u) ;}} int main () {int n; while (scanf ("% d", & n) = 1) {int w, v; MS0 (head); MS0 (dp); tot = 0; rep1 (I, 2, n) {read2 (v, w ); ins (I, v, w); ins (v, I, w);} dfs (1,-1); solve (1,-1); rep1 (I, 1, n) {out (dp [I] [0]); puts ("") ;}} return 0 ;}