您的位置:首页 > 其它

bzoj 2196 Computer(树形DP)

2016-05-04 20:22 302 查看

Computer

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5242 Accepted Submission(s): 2646



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).

[align=left]Sample Input[/align]

5
1 1
2 1
3 1
1 1


[align=left]Sample Output[/align]

3
2
3
4
4


[align=left]Author[/align]
scnu

[align=left]Recommend[/align]
lcy | We have carefully selected several similar problems for you: 1561 1011 3456 1520 2242

Statistic | Submit | Discuss
| Note

题目大意:求树上的每个点到离他最远的点的距离

题解:树形dp

maxn[i] 表示子树中的最长链

_maxn[i] 表示子树中的次长链

f[i] 表示子树中的最长距离

g[i] 表示子树外的最长距离

ans[i] 表示最长距离

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 20003
using namespace std;
int n,m;
int point
,nxt
,v
,tot,c
;
int f
,g
,maxn
,_maxn
,ans
;
void add(int x,int y,int z)
{
tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; c[tot]=z;
tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; c[tot]=z;
}
int dfs(int x,int fa)//求出最长链和次长链
{
for (int i=point[x];i;i=nxt[i])
if (v[i]!=fa)
{
int t=c[i]+dfs(v[i],x);
if (t>maxn[x])
_maxn[x]=maxn[x],maxn[x]=t;
else
if (t>_maxn[x])
_maxn[x]=t;
}
return maxn[x];
}
void get_ans(int x,int fa)
{
for (int i=point[x];i;i=nxt[i])
if(v[i]!=fa)
{
f[v[i]]=maxn[v[i]];//子树中的最长距离
if (maxn[x]==maxn[v[i]]+c[i])//如果当前子节点在他的最长链中,只能用次长链和子树外的最长距离更新
g[v[i]]=c[i]+max(_maxn[x],g[x]);
else
g[v[i]]=c[i]+ans[x];
ans[v[i]]=max(g[v[i]],f[v[i]]);
get_ans(v[i],x);
}
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
tot=0;
memset(nxt,0,sizeof(nxt));
memset(point,0,sizeof(point));
for (int i=2;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(i,x,y);
}
for (int i=1;i<=n;i++)
f[i]=0,g[i]=0,maxn[i]=0,_maxn[i]=0;
dfs(1,0);
ans[1]=f[1]=maxn[1];
get_ans(1,0);
for (int i=1;i<=n;i++)
printf("%d\n",ans[i]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: