hdu2196 Computer[树形dp]
Posted 神犇(shenben)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu2196 Computer[树形dp]相关的知识,希望对你有一定的参考价值。
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6716 Accepted Submission(s): 3361
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
Author
scnu
Recommend
MX表示最大值
mx表示次大值
转移;1、其子树
2、经过根
#include<cstdio> #include<cstring> #include<iostream> using namespace std; const int N=1e4+5; struct edge{int v,w,next;}e[N<<1];int tot,head[N]; int MX[N],MAXid[N]; int mx[N],maxid[N]; int n,m; inline void add(int x,int y,int z){ e[++tot].v=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot; e[++tot].v=x;e[tot].w=z;e[tot].next=head[y];head[y]=tot; } void PreDfs(int x,int f){ MX[x]=mx[x]=0; for(int i=head[x],y;i;i=e[i].next){ if((y=e[i].v)==f) continue; PreDfs(y,x); if(mx[x]<MX[y]+e[i].w){ mx[x]=MX[y]+e[i].w; maxid[x]=y; if(mx[x]>MX[x]){ swap(MX[x],mx[x]); swap(MAXid[x],maxid[x]); } } } } void SolDfs(int x,int f){ for(int i=head[x],y;i;i=e[i].next){ if((y=e[i].v)==f) continue; if(y==MAXid[x]){ if(mx[y]<mx[x]+e[i].w){ mx[y]=mx[x]+e[i].w; maxid[y]=x; if(mx[y]>MX[y]){ swap(MX[y],mx[y]); swap(MAXid[y],maxid[y]); } } } else{ if(mx[y]<MX[x]+e[i].w){ mx[y]=MX[x]+e[i].w; maxid[y]=x; if(mx[y]>MX[y]){ swap(MX[y],mx[y]); swap(MAXid[y],maxid[y]); } } } SolDfs(y,x); } } int main(){ while(~scanf("%d",&n)){ tot=0;memset(head,0,sizeof head); for(int i=2,y,z;i<=n;i++) scanf("%d%d",&y,&z),add(i,y,z); PreDfs(1,-1); SolDfs(1,-1); for(int i=1;i<=n;i++) printf("%d\n",MX[i]); } return 0; }
以上是关于hdu2196 Computer[树形dp]的主要内容,如果未能解决你的问题,请参考以下文章