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.
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
题意: 给出一棵树,求这颗树每个节点的最大距离。
下面我们来具体分析下,这个点的在树中的最大距离如何计算?
下面我们按照这棵树来分析:
例如:我们要求5号点的最大距离,肉眼观察,很显然最大距离是4,其路径是5->7->8->10->12.
1、首先我们以5号点为根,把树看作2部分,分成上和下。
如图所示:
2、我们很容易能算出下面部分的最大距离,只要dfs一遍就能求出。
3、本题最核心的地方是求上半部分的最大值,也是我们树形dp的思想。
分2种情况:
①第一种情况就是当前节点经过父亲节点的最大距离
比如上面的5号节点,他的父亲节点是2,并且2号节点的最大距离路径是2->5->7->8->10->12. 可以发现,2号节点的最大长度的路径中有5号节点,
那么 5号节点的上半部分的最大距离=max(2号节点的下半部分次大距离+权值,2号节点上半部分最大距离+权值).
那么为什么要次大距离呢??????????小朋友,你是不是有很多问号?
因为 我们当前节点是经过它父节点的最大距离,所以它已经在最大距离上了。因为次大距离上,一定没有5号点。所以是次大距离
请看上图,5号点是经过2号点的最大距离的路径上,那么 2号到达4号的就是2号点向下的次大距离,加上权值,路径是max(5->2->4,5->2-1).
如果还是有点模糊的话,请看第二个例子:
求7号点的最大距离: (7号节点的父节点是5号)
=Max(5号节点的下半部分次大距离+权值,5号节点上半部分的最大距离+权值)
5号节点的下半部分次大距离+权值:
*请读者好好品味下。。。
②第二种情况就是当前节点不经过父亲节点的最大距离
不经过的话,很简单,当前节点上半部分的最大距离=max(父亲节点的向下的最大距离+权值,父亲节点的向上的最大距离+权值)
例如,现在我们要计算4号节点。
下图是父亲节点向下的最大距离为5
父亲节点向上的最大距离为1
如图:
接下来就是代码如何写:
1、dfs1求出每个点向下的最大距离和次大距离
2、dfs2求出每个点向上的次大距离
3、答案max(当前点向上最大距离,当前点向下最大距离)
AC代码,有详细注释(注意long long)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=10000+100;
ll head[N<<1],cnt;
ll dp[N][3];//dp[i][0]:节点i向下的最大距离 dp[i][1]:节点i向下的次大距离 dp[i][2]:节点i向上的最大距离
ll vis[N];//vis[u]=v 表示节点v经过节点u向下的最大距离
ll n,v,w;
struct Edge
{
ll e;
ll to;
ll data;
}edge[N<<1];
void add_edge(ll u,ll v,ll val)
{
cnt++;
edge[cnt].e=v;
edge[cnt].to=head[u];
edge[cnt].data=val;
head[u]=cnt;
}
void init()
{
memset(head,0,sizeof(head));
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
cnt=0;
}
void dfs1(ll u,ll f)//第一次dfs1 处理节点i向下的最大距离和次大距离
{
ll max1=0;//最大距离
ll max2=0;//次大距离
for(ll i=head[u];i!=0;i=edge[i].to)
{
ll v=edge[i].e;
if(v==f)continue;
dfs1(v,u);
if(edge[i].data+dp[v][0]>=max1)
{//如果当前父亲节点的权值+向下最大距离>=max1
max2=max1;//首先更新次大距离,次大距离就是等于上次的最大距离
max1=edge[i].data+dp[v][0];//再更新最大距离
vis[u]=v;//标记节点v经过节点u的最大距离
}
else if(edge[i].data+dp[v][0]>max2)
{//如果当前父亲节点的权值+向下最大距离>=max2
max2=edge[i].data+dp[v][0];//更新次大距离
}
else if(edge[i].data+dp[v][1]>max2)
{//如果当前父亲节点的权值+向下次大距离>=max2
max2=edge[i].data+dp[v][1];//更新次大距离
}
}
//最大距离和次大距离存入
dp[u][0]=max1;
dp[u][1]=max2;
}
void dfs2(ll u,ll f)
{
for(ll i=head[u];i!=0;i=edge[i].to)
{
ll v=edge[i].e;
if(v==f)continue;
if(vis[u]==v)
{//当前结点v经过u的最大距离
dp[v][2]=max(dp[u][1]+edge[i].data,dp[u][2]+edge[i].data);
//结点v向上的最大距离=MAX(父节点u的向下次大距离+(v,u)的权值 , 父节点u向上的最大距离+(v,u)的权值)
}
else
{//当前结点v不经过u的最大距离
dp[v][2]=max(dp[u][0]+edge[i].data,dp[u][2]+edge[i].data);
//结点v向上的最大距离=MAX(父节点u的向下最大距离+(v,u)的权值 , 父节点u向上的最大距离+(v,u)的权值)
}
dfs2(v,u);
}
}
int main()
{
while(~scanf("%lld",&n))
{
init();
for(ll u=2;u<=n;u++)
{
scanf("%lld%lld",&v,&w);
add_edge(u,v,w);
add_edge(v,u,w);
}
dfs1(1,1);
dfs2(1,1);
for(ll i=1;i<=n;i++)
{
printf("%lld\n",max(dp[i][0],dp[i][2]));
}
}
return 0;
}
愿,自己不再辜负自己。