HDU 2196-Computer(经典树形DP)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2196

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7329    Accepted Submission(s): 3630


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
 
题意是求树中每个点到所有叶子节点的距离的最大值是多少。


//经典树形DP
//进行第一次dfs,先以1为根,求出每个节点以该节点为根的子树的最大值和第二大的值
//进行第二次dfs, 对于每个节点v的最大值与第二大值,可能是来自自己的子树或者父节点来的
//判断它来自哪,这取决于该节点v父亲fa[v]取的最大值是来自哪个节点book[v]
//如果book[v]==v,说明fa[v]取的最大值来源于节点v,所以v节点的最大值来源不能是fa[v]的最大值,只能来源于fa[v]第二大或者它的子节点
//如果book[v]!=v, 说明fa[v]取的最大值不是来源于节点v,所以v节点的最大值来源可以是fa[v]的最大值也可以是它的子节点,找最大的
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define maxn 10005
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;

struct node
{
	int v, w;
};

vector<node>G[maxn];
ll dp[maxn][2];                     //0是最大,1是第二大
int fa[maxn];                       //父亲
int book[maxn];                     //book[i]表示节点i的最大距离的路径经过的第一个点
//ll num[maxn];//存最终结果
//int vis[maxn];

void dfs1(int root)
{
	int l = G[root].size();
	for(int i=0; i<l; i++)
	{
		int to = G[root][i].v;
		dfs1(to);
	}

	//找第一大
	int num1 = 0;
	for(int i=0; i<l; i++)
	{
		node to = G[root][i];
		if(dp[root][0] < dp[to.v][0]+to.w)
		{
			num1 = i;
			dp[root][0] = dp[to.v][0]+to.w;
			book[root] = to.v;
		}
	}
	//找第二大
	for(int i=0; i<l; i++)
	{
		node to = G[root][i];
		if(dp[root][1] < dp[to.v][0]+to.w && num1!=i)
		{
			dp[root][1] = dp[to.v][0]+to.w;
		}
	}

}

void dfs2(int root, int cnt)            //cnt表示root是它父节点的第cnt个孩子
{
    if(root!=1)
    {
    if(book[fa[root]]!=root)
    {
        if(dp[fa[root]][0]+G[fa[root]][cnt].w > dp[root][0])
        {
            dp[root][1] = dp[root][0];
            dp[root][0] = dp[fa[root]][0]+G[fa[root]][cnt].w;
            //printf("fsafa== %d\n", dp[root][0]);
            book[root] = fa[root];
        }
        else
        {
            dp[root][1] = max(dp[root][1],dp[fa[root]][0]+G[fa[root]][cnt].w);
        }
    }
    else
    {
        if(dp[fa[root]][1]+G[fa[root]][cnt].w > dp[root][0])
        {
            dp[root][1] = dp[root][0];
            dp[root][0] = dp[fa[root]][1]+G[fa[root]][cnt].w;
            book[root] = fa[root];
        }
        else
        {
            dp[root][1] = max(dp[root][1],dp[fa[root]][1]+G[fa[root]][cnt].w);
        }
    }
    }
    int l = G[root].size();
    for(int i=0; i<l; i++)
        dfs2(G[root][i].v, i);
}
/*
void dfs3(int root, int cnt)          //cnt表示root是它父节点的第cnt个孩子
{
	int l = G[root].size();
	for(int i=0; i<l; i++)
	{
		int to = G[root][i].v;
		dfs3(to, i);
	}
    if(root==1)
    {
        num[root] = dp[root][0];
        return;
    }
    if(book[fa[root]]!=root)
    {
        num[root] = max(dp[root][0], dp[fa[root]][0]+G[fa[root]][cnt].w);
    }
    else
    {
        num[root] = max(dp[root][0], dp[fa[root]][1]+G[fa[root]][cnt].w);
    }
}
*/
int main()
{
	int n;
	while(~scanf("%d", &n))
	{
		for(int i=0; i<maxn; i++)
        {
            fa[i] = 0;
            book[i] = 0;
            num[i] = 0;
            dp[i][0] = dp[i][1] = 0;
            G[i].clear();
        }
		for(int i=0; i<n-1; i++)
		{
			int u, w;
			scanf("%d%d", &u, &w);
			G[u].push_back((node){i+2, w});
			//G[i+2].push_back((node){u, w});
			fa[i+2] = u;
		}
//		printf("yes\n");
		dfs1(1);
//		printf("yes1\n");
		dfs2(1,0);
//		printf("yes2\n");
//		dfs3(1,0);
//		printf("yes3\n");
		for(int i=1; i<=n; i++)
		{
		   // printf("dp[%d][0] = %lld\n", i, dp[i][0]);
		    //printf("dp[%d][1] = %lld\n", i, dp[i][1]);
		    //printf("book[%d] = %d\n", i, book[i]);
			printf("%lld\n", dp[i][0]);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值