[Description]
 
Given a tree with N nodes, calculate the maximum distance between each node and other nodes.
[Input format]
The first line of the input is a natural number N (N <= 10000), followed by the (N-1) line description:
Line I contains two natural numbers, indicating the node number connected to the node numbered I and the length of the network cable ..
The total length cannot exceed 10 ^ 9. Two numbers in each line are separated by spaces.
[Output format]
The output contains N rows. line I indicates the Si distance between the node that is farthest from the node with ID I.
(1 <= I <= N ).
[Example input] length. in
3
1 1
1 2
[Sample output] length. out
2
3
3
[Data Scope]
30% N <= 100
100% N <= 10000
 
Analysis:
 
Due to limited data range,
 
It also conforms to the structural features of the tree and can be done using dfs,
 
3 times dfs output.
 
View Code
 
1 program length;
2 var
3 I, ww, j, n, m, k, w, mm, t: Longint;
4 x, y, z: longint;
5 a, ll: array [1 .. 10010, 1 .. 500] of longint;
6 d1, d2, l, v: array [0 .. 10010] of longint;
7 // a records the edge length.
8 // ll records the number of vertex connected by edge
9 procedure dfs (x, dis: longint );
10 var
11 I, j, bb: longint;
12 begin
13 bb: = 0; v [x]: = 1;
14 for I: = 1 to l [x] do
15 if (v [ll [x, I] = 0) then
16 begin
17 bb: = 1;
18 dfs (ll [x, I], dis + a [x, I]);
19 end;
20
21 if (bb = 0) and (dis> m) then begin w: = x; m: = dis; end;
22 end;
23 // The first dfs, starting from any point (I selected point 1,
24 // find the farthest point from this point
25
26 procedure dfss (x, dis: longint );
27 var
28 I, j, bb: longint;
29 begin
30 d1 [x]: = dis;
31 bb: = 0; v [x]: = 1;
32 for I: = 1 to l [x] do
33 if v [ll [x, I] = 0 then
34 begin
35 bb: = 1;
36 dfss (ll [x, I], dis + a [x, I]);
37 end;
38
39 if (bb = 0) and (dis> m) then begin w: = x; m: = dis; end;
40 end;
41 // second dfs
42 // find the tree diameter (that is, the two points in the tree that are farthest from each other)
43 // same as the "tree diameter" question in tyvj (that question only needs two dfs requests)
44
45 procedure dfsss (x, dis: longint );
46 var
47 I, j, bb: longint;
48 begin
49 d2 [x]: = dis;
50 bb: = 0; v [x]: = 1;
51 for I: = 1 to l [x] do
52 if v [ll [x, I] = 0 then
53 begin
54 bb: = 1;
55 dfsss (ll [x, I], dis + a [x, I]);
56 end;
57 end;
58 // third time dfs
59 // locate the distance from the other end of the tree to each point in the tree
60
61 function max (x, y: longint): longint;
62 begin
63 if x> = y then exit (x)
64 else exit (y );
65 end;
66
67 begin
68 assign (input, 'length. in ');
69 reset (input );
70 assign (output, 'length. out ');
71 rewrite (output );
72 readln (n );
73 for I: = 2 to n do
74 begin
75 readln (x, y );
76 inc (l [I]); a [I, l [I]: = y; ll [I, l [I]: = x;
77 inc (l [x]); a [x, l [x]: = y; ll [x, l [x]: = I;
78 end;
79 // use the pseudo Matrix Method to read data (some space is wasted, but it is less space than the pure matrix, because the edge of the tree is limited, generally there is no limit data card)
80 // note: the pure matrix storage is out of memory, so I won't list it, so I have to save it like this
81
82 fillchar (v, sizeof (v), 0 );
83 m: = 0;
84 dfs (1, 0 );
85
86 fillchar (v, sizeof (v), 0 );
87mm: = m; m: = 0;
88 ww: = w;
89 dfss (ww, 0 );
90 // obtain the diameter. w and ww are the two vertices respectively.
91
92 fillchar (v, sizeof (v), 0 );
93 dfsss (w, 0 );
94
95 for I: = 1 to n do writeln (max (d1 [I], d2 [I]);
96 // The longest distance that any vertex in the tree can reach must be one of the two vertices.
97 close (input );
98 close (output );
99 end.