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.
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
题意:求任意一点在树上的最长路径,比如1节点和4节点的最长路径是3
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const int maxn=10005; 5 int pos; 6 int n,ans,vis[maxn],in[maxn]; 7 vector<pair<int,int>>e[maxn]; 8 ll sum; 9 void dfs(int v,int cnt) 10 { 11 if(ans<cnt) 12 { 13 ans=cnt; 14 pos=v; 15 } 16 if(vis[v])return; 17 vis[v]=1; 18 for(int i=0; i<e[v].size(); i++) 19 // cout<<e[v][i].first; 20 if(!vis[e[v][i].first]) 21 dfs(e[v][i].first,cnt+e[v][i].second); 22 } 23 int dis1[maxn],dis2[maxn]; 24 void DFS(int v,int cnt,int dis[]) 25 { 26 if(vis[v]) return; 27 vis[v]=1; 28 dis[v]=cnt; 29 for(int i=0; i<e[v].size(); i++) 30 // cout<<e[v][i].first; 31 if(!vis[e[v][i].first]) 32 DFS(e[v][i].first,cnt+e[v][i].second,dis); 33 } 34 int main() 35 { 36 int n,m; 37 ans=0; 38 while(~scanf("%d",&n)) 39 { 40 ans=0; 41 memset(dis1,0,sizeof(dis1)); 42 memset(dis2,0,sizeof(dis2)); 43 memset(in,0,sizeof(in)); 44 memset(vis,0,sizeof(vis)); 45 for(int i=0;i<=n;i++) 46 { 47 e[i].clear(); 48 } 49 for(int i=2; i<=n; i++) 50 { 51 int u,v,w; 52 scanf("%d%d",&v,&w); 53 e[i].push_back({v,w}); 54 e[v].push_back({i,w}); 55 } 56 dfs(2,0); 57 int cnt=ans; 58 ans=0; 59 memset(vis,0,sizeof(vis)); 60 ans=0; 61 DFS(pos,0,dis1); 62 memset(vis,0,sizeof(vis)); 63 ans=0; 64 dfs(pos,0); 65 66 memset(vis,0,sizeof(vis)); 67 DFS(pos,0,dis2); 68 memset(vis,0,sizeof(vis)); 69 int cot=ans; 70 //cout<<cot<<" "<<cnt<<endl; 71 int Max=max(cnt,cot); 72 //cout<<Max<<endl; 73 sum=0; 74 for(int i=1;i<=n;i++) 75 { 76 printf("%d ",max(dis1[i],dis2[i])); 77 } 78 } 79 return 0; 80 }
Author
scnu