ComputerTime
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3956 Accepted Submission (s): 1983
Problem Descriptiona 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 is connected to one of settled earlier. Managers of school is anxious about slow functioning of the net and want to know the maximum distance Si for which i-th C Omputer needs to send signal (i.e. length of cable-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 are farthest one from 1, so S1 = 3. Computer 4 and 5 is 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.
Inputinput file contains multiple test cases. In each case there was natural number N (n<=10000) in the first line, followed by (N-1) lines with descriptions of Compu Ters. I-th line contains-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 is separated by a space.
Outputfor each case output N lines. I-th line must contain number Si for i-th computer (1<=i<=n).
Sample Input
51 12 13) 11 1
Sample Output
32344
Find the tree on a point to other points of the furthest distance, first with any point for the root node, a BFS search to the current point of the furthest point root1, starting from root1 to search, get Root2, from Root2 again search to get the final answer, each search process update each point of the furthest distance.
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < algorithm> #include <queue> #include <vector>using namespace std; #define LL Long Long#define N 11000# Define MEM (a,t) memset (A,t,sizeof (a)) struct node{int v,w,next;} E[n*2];int head[n];int dis[n];int cnt;int len_max,root;void Add (int u,int v,int W) {e[cnt].v=v; E[cnt].w=w; E[cnt].next=head[u]; head[u]=cnt++;} void BFs (int u,int len,int fa) {int i,v; if (Len>len_max) {Len_max=len; Root=u; } for (I=head[u];i!=-1;i=e[i].next) {v=e[i].v; if (V!=FA) {if (DIS[V]<LEN+E[I].W) DIS[V]=LEN+E[I].W; BFS (V,len+e[i].w,u); }} return; int main () {int i,n,a,b; while (~SCANF ("%d", &n)) {mem (head,-1); cnt=0; for (i=2;i<=n;i++) {scanf ("%d%d", &a,&b); Add (i,a,b); Add (a,i,b); }MEM (dis,0); len_max=0; BFS (1,0,-1); len_max=0; BFS (root,0,-1); BFS (root,0,-1); for (i=1;i<=n;i++) printf ("%d\n", Dis[i]); } return 0;}
HDU 2196 Computer