Computer(树上最远距离 树形DP)
Posted qq-1585047819
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Computer(树上最远距离 树形DP)相关的知识,希望对你有一定的参考价值。
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 38859 Accepted Submission(s): 7140
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 #include <bits/stdc++.h> 2 using namespace std; 3 4 int n; 5 const int maxn=1e4+5; 6 struct Edge{ 7 int v,w,next; 8 }edge[maxn<<1]; 9 int f[maxn],g[maxn],fa[maxn]; //f第一步往下走的走远距离,g第一步往往上走的组就远距离 10 11 int head[maxn],cnt; 12 void init(){ memset(head,-1,sizeof(head)),cnt=0; for(int i=1;i<maxn;i++) f[i]=g[i]=fa[i]=0; } 13 void add(int u,int v,int w){ 14 edge[cnt].v=v; 15 edge[cnt].w=w; 16 edge[cnt].next=head[u]; 17 head[u]=cnt++; 18 } 19 20 int dfs(int u,int pre){ ///自底往上 21 fa[u]=pre; 22 for(int i=head[u];~i;i=edge[i].next){ 23 int x=edge[i].v; 24 if(x!=pre){ 25 f[u]=max(f[u],edge[i].w+dfs(x,u)); 26 } 27 } 28 return f[u]; 29 } 30 31 void dfs2(int u,int pre){ ///自上往下 32 g[u]=g[pre]; 33 int t=0; 34 for(int i=head[pre];~i;i=edge[i].next){ 35 int v=edge[i].v,w=edge[i].w; 36 if(v==fa[pre]) continue; //祖先的祖先 37 if(v==u) t=w; 38 else g[u]=max(g[u],f[v]+w); 39 } 40 // if(pre!=0) 41 g[u]+=t; 42 for(int i=head[u];~i;i=edge[i].next){ 43 if(edge[i].v!=pre) dfs2(edge[i].v,u); 44 } 45 } 46 47 int main(){ 48 while(~scanf("%d",&n)){ 49 init(); 50 for(int i=2,d1,d2;i<=n;i++){ 51 scanf("%d%d",&d1,&d2); 52 add(i,d1,d2); 53 add(d1,i,d2); 54 } 55 dfs(1,0); 56 dfs2(1,0); 57 58 for(int i=1;i<=n;i++){ 59 printf("%d ",max(f[i],g[i])); 60 } 61 } 62 return 0; 63 }
以上是关于Computer(树上最远距离 树形DP)的主要内容,如果未能解决你的问题,请参考以下文章