Computer
**Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6378 Accepted Submission(s): 3211
**
Problem Description
A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.
Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.
Output
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
Sample Input
5
1 1
2 1
3 1
1 1
Sample Output
3
2
3
4
4
Author
scnu
Recommend
lcy | We have carefully selected several similar problems for you: 1011 3456 1520 2242 1059
题意:给n个点,1为根节点,给你的是棵树,然后找到各个节点离树上其他节点最远的距离;
思路:树形dp;
因为这是一颗树,除了根节点外其他每个节点都又且一个父亲节点,首先dfs更新每个节点到其子节点下端的最长距离和次长距离,然后最长距离还可能从父亲节点那更新。那么再dfs从父亲节点更新子节点的最长距离,这个时候,父亲节点的最长可能是通过当前要更新的子节点,那么就用次长路去更新。复杂度O(n);
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include