HDU 2196 Computer 树形DP

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

题意:给出一棵树,求出每个节点在树上的最远距离。

分析:每个节点i的最远距离有两种情况:i节点子树中最远距离,i节点经过其父节点后可以到达的最远距离,需要进行两次dfs。

第一次dfs求每个节点i在其子树中的正向最长距离dp[i][0]和正向次长距离dp[i][1],并用longest[i]=j记录节点i在其子树中最长距离经过的子节点j。

第二次dfs求反向最长距离dp[i][2],即i经过其父节点j后所能到达的最大距离,等于父节点j的反向最大距离和正向不经过节点i的最大距离的最大值+i与j的距离,其中不经过节点i的正向最大距离通过判断longest[j]是否等于i来取dp[j][1]或dp[j][0]。

 

#include<iostream>
#include<algorithm>
#include<cstring>
#define mem(a,b) memset(a,b,sizeof(a))

using namespace std;
const int maxn=10005;
struct Edge{
	int u;//邻接点 
	int len;//电缆长度 
	int next;//下一条边 
	Edge() {}
	Edge(int u,int len,int next)
	:u(u),len(len),next(next){}
}edge[maxn];

int N,num,head[maxn],dp[maxn][3],longest[maxn];

void init()
{
	mem(dp,0);
	mem(edge,0);
	mem(head,-1);
	mem(longest,-1);
	num=0;
	
}
void add(int v,int u,int len)
{
	edge[num]=Edge(u,len,head[v]);
	head[v]=num++;
}

void dfs1(int v)
{
	for(int i=head[v];i!=-1;i=edge[i].next)
	{
		int u=edge[i].u;
		dfs1(u);
		int maxLen=dp[u][0]+edge[i].len;//经过子节点u的最长距离 
		if(maxLen>dp[v][0])
		{//大于最长距离 
			dp[v][1]=dp[v][0];
			dp[v][0]=maxLen;
			longest[v]=u;//记录正向最长距离经过的子节点 
		}
		else if(maxLen>dp[v][1])
		{//大于次长距离 
			dp[v][1]=maxLen;
		}
	}
}

void dfs2(int v)
{
	for(int i=head[v];i!=-1;i=edge[i].next)
	{
		int u=edge[i].u;
		dp[u][2]=edge[i].len+max(longest[v]==u?dp[v][1]:dp[v][0],dp[v][2]);//节点u经过父节点v的反向最长距离 
		dfs2(u);
	}
}
int main()
{
	while(cin>>N)
	{
		init();//初始化 
		for(int u=2;u<=N;u++)
		{
			int v,l;
			scanf("%d%d",&v,&l);
			add(v,u,l);
		}
		dfs1(1); //求解正向最长距离和次长距离 
		dfs2(1);//反向最长距离 
		for(int i=1;i<=N;i++)
			printf("%d\n",max(dp[i][0],dp[i][2]));
	}
	return 0;
	
}

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值