Computer HDU - 2196(树形dp)

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
思路:首先我们以1为根跑一遍,记录一下每一个点到它的子树的最长边和次长边。这是第一次dfs要干的事情,这样我们可以求出点1的答案,但是其他的无法求出来,除非我们以其他的点为根再跑一遍,这样必定超时。因此考虑树形dp。参考一下这个样例:
在这里插入图片描述
具体看代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
struct edge{
	int to,next,val;
}e[maxx<<1];
int head[maxx<<1],fa[maxx],dp1[maxx],dp2[maxx],ans[maxx];
int n,tot;

inline void init()
{
	memset(head,-1,sizeof(head));
	for(int i=0;i<=n+1;i++) dp2[i]=dp1[i]=fa[i]=0;
	tot=0;
}
inline void add(int u,int v,int w)
{
	e[tot].next=head[u],e[tot].to=v,e[tot].val=w,head[u]=tot++;
}
inline void dfs(int u,int f)//这个dfs求得是每个点,以它为祖先的子树中,最长距离和次长距离
{
	fa[u]=f;
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		dfs(to,u);
		if(dp1[to]+e[i].val>dp1[u])
		{
			dp2[u]=dp1[u];
			dp1[u]=dp1[to]+e[i].val;
		}
		else dp2[u]=max(dp2[u],dp1[to]+e[i].val);
	}
}
inline void dfs1(int u,int f,int num)//在这个dfs中,次长距离dp2已经更新过了,有可能不是原来的次长距离了,因此有可能会比dp1大,这是正常的。
{
	if(dp1[f]==dp1[u]+num) dp2[u]=max(dp2[u],dp2[f]+num);//这个点在父节点的最长边上,那么就更新这个点的次长边,有可能还是原来的次长边,也有可能是父节点的次长边+他们之间的距离。
	else dp2[u]=max(dp2[u],max(dp1[f],dp2[f])+num);//如果不在父节点的最长边上,那么这个点的次长边有可能还是原来的,也有可能是父节点中最长边和次长边取最大值+他们之间的距离(ps:已经不知道dp1和dp2哪个长了,需要取最大值)。
	//经过上面的操作,最长边和次长边已经无法知道了,因此不止是这个点子树的距离,也加上了父节点和其他兄弟节点之间的距离比较。自己画个图理解一下。
	if(u!=1) ans[u]=max(dp1[u],dp2[u]);//答案在这两者之间取得。
	else ans[u]=dp1[u];
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		dfs1(to,u,e[i].val);
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		init();
		int x,y;
		for(int i=2;i<=n;i++)
		{
			scanf("%d%d",&x,&y);
			add(i,x,y);
			add(x,i,y);
		}
		dfs(1,0);
		dfs1(1,0,0);
		for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
	}
	return 0;
}

努力加油a啊,(o)/~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值