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.
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
题目大意:
给你N个点的一棵树,问距离每一个点最远的点的距离。
思路:
①我们设定Dp【u】表示以u为根的子树中,距离点u最远距离长度。
那么有:Dp【u】=max(Dp【v】+W(u,v));
②现在对于一个点u来讲,有两个方向,一个是子树方向,一个是非子树方向。
我们搞定了子树方向,就剩下非子树方向了。
那么设定F【u】表示非子树方向到点u节点最远的长度。
对于一个点来说,如果是非子树方向就有两种递推状态的方案:
1.从父亲节点来(从根节点到当前节点这条路径的长度):F【u】=F【from】+W(from,u);
2.从当前节点的兄弟节点而来;F【u】=max(Dp【brother】+W(from,u)+W(from,v));
如果存在一个点有多个儿子的情况,从2状态转移的时间复杂度会达到O(n^2),很危险,所以我们再维护一个数组Id【u】表示以u为根的子节点中Dp【v】值最大的节点编号。同时维护从这个点u到这个最大值点v直接路径的长度(W(u,v));
过程维护一下即可。
代码:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
int from;
int to;
int w;
int next;
}e[35000];
int head[15000];
int fa[15000];
int maxn[15000];
int Id[15000];
int F[15000];
int dp[15000];
int n,cont;
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void dfs(int u,int from)
{
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(v==from)continue;
else
{
dfs(v,u);
if(dp[v]+w>dp[u])
{
dp[u]=dp[v]+w;
Id[u]=v;
}
}
}
}
void Dfs(int u,int from,int prew)
{
fa[u]=from;
if(from!=-1)F[u]=max(F[from]+prew,F[u]);
if(from!=-1)
{
if(u==Id[from])
{
for(int i=head[from];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(v==u)continue;
if(from!=-1&&v==fa[from])continue;
if(v==from)continue;
F[u]=max(dp[v]+prew+w,F[u]);
}
}
else
{
F[u]=max(F[u],dp[from]+prew);
}
}
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(v==from)continue;
Dfs(v,u,w);
}
}
int main()
{
while(~scanf("%d",&n))
{
cont=0;
memset(head,-1,sizeof(head));
memset(F,0,sizeof(F));
memset(dp,0,sizeof(dp));
for(int i=2;i<=n;i++)
{
int faa,w;
scanf("%d%d",&faa,&w);
add(i,faa,w);
add(faa,i,w);
}
dfs(1,-1);
Dfs(1,-1,0);
/*
for(int i=1;i<=n;i++)
{
printf("%d ",dp[i]);
}
printf("\n");
for(int i=1;i<=n;i++)
{
printf("%d ",F[i]);
}
printf("\n");*/
for(int i=1;i<=n;i++)
{
printf("%d\n",max(F[i],dp[i]));
}
}
}