hdu 2196 Computer(树形DP)

原题地址:点我跳转

Computer

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


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

题目的大意是,给你一棵树,求每个节点的最远的距离。

我们把最开始的点作为根节点(电脑1),建立有根树。先用深搜遍历一遍树,求出对于每个父节点,其最远的子叶子节点的最远距离(下文简称为最远子距离maxs[i])和不同路径的其他子叶子节点的次远距离。


如上图,1的最远子叶子节点的距离是1-2-5-6  ,是5。不同路径上的次远子距离(下文简称为次远子距离sec[i])是1-3,是3。而不是1-2-4 ,因为节点2有重复。

深搜最远距离和次远距离的同时,记录好最远距离的直接子节点编号。


第二遍深搜,我们进行DP,求出答案。思路如下:
每次进行dp时,以当前搜索到的点为界,将这棵树分为两部分,如下图:

以6号点为例,节点6的最远距离,要么是节点6最远子距离(6-9)maxs[6],要么是红圈部分的树上节点3的最远距离加上节点3到节点6的距离(5-2-1-3-6)+dist(3,6) (下文简称这个距离为局部最远距离dp[i])

因此我们需要求得所有节点的局部最远距离dp[i]。
局部最远距离dp[i],可以由父节点的局部最远距离推导。

设父节点为par,子节点为son。
①如果son不是par的最远子距离上的点,例如上图的4和3。3的最远子距离maxs[3] = 6(3-6-9),节点4不在上面
dp[son] = dist(par, son)+ max(maxs[par], dp[par])
②如果son是par的最远子距离上的点,例如上图的6和3。3的最远子距离maxs[3] = 6(3-6-9),节点6在上面
dp[son] = dist(par, son)+ max(sec[par], dp[par])

而最后的答案,就是max(dp[i], maxs[i])

AC代码:
#include<cstdio>
#include<cstdlib>
#include <algorithm>
#include<cmath>
using namespace std;

struct node
{
    int dp;//局部最远距离
    int ans;//最后答案
    int maxs;//最远子距离
    int sec;//次远子距离
    int len;//该节点到父节点的距离dist(son,par)
    int son;//孩子
    int maxson;//最远子距离的直接孩子
    int bro;//兄弟
} tree[10010];

void dfs(int root)//深搜最远子距离和次远子距离
{
    if(tree[root].son==0)
        return;
    else
    {
        int p;
        p=tree[root].son;
        dfs(p);
        if(tree[root].maxs<tree[p].len+tree[p].maxs)
        {
            tree[root].sec = tree[root].maxs;
            tree[root].maxs = tree[p].len+tree[p].maxs;
            tree[root].maxson = p;
        }
        else if(tree[root].sec<tree[p].len+tree[p].maxs)
        {
            tree[root].sec = tree[p].len+tree[p].maxs;
        }
        //printf("!root=%d maxs=%d maxson=%d sec=%d\n",root,tree[root].maxs,tree[root].maxson,tree[root].sec);
        while(tree[p].bro!=0)
        {
            int tmp=p;
            p=tree[p].bro;
            dfs(p);
            if(tree[root].maxs<tree[p].len+tree[p].maxs)
            {
                tree[root].sec = tree[root].maxs;
                tree[root].maxs = tree[p].len+tree[p].maxs;
                tree[root].maxson = p;
            }
            else if(tree[root].sec<tree[p].len+tree[p].maxs)
            {
                tree[root].sec = tree[p].len+tree[p].maxs;
            }
            //printf("root=%d maxs=%d maxson=%d sec=%d\n",root,tree[root].maxs,tree[root].maxson,tree[root].sec);
        }
    }
    return;
}

void dfsAns(int son,int par)//深搜+DP
{
    if(son==1)
    {
        tree[son].dp = 0;
        tree[son].ans = tree[son].maxs;
    }
    else
    {
        int maxlen = tree[par].maxs;
        if(tree[par].maxson==son)
        {
            maxlen = tree[par].sec;
        }
        maxlen = max(tree[par].dp,maxlen);
        tree[son].dp = maxlen + tree[son].len;
        tree[son].ans = max(tree[son].dp,tree[son].maxs);

    }
    if(tree[son].son==0)
        return ;
    else
        dfsAns(tree[son].son,son);
    int p = tree[son].son;
    while(tree[p].bro!=0)
    {
        p = tree[p].bro;
        dfsAns(p,son);
    }
}

int main()
{
    int n;
    int f,l;
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
        {
            tree[i].dp=0;
            tree[i].ans=0;
            tree[i].maxs=0;
            tree[i].sec=0;
            tree[i].son=0;
            tree[i].maxson=0;
            tree[i].bro=0;
        }
        int p;
        for(int i=2; i<=n; i++)//左孩子右兄弟建树
        {
            scanf("%d%d",&f,&l);
            if(tree[f].son==0)
                tree[f].son=i;
            else
            {
                p=tree[f].son;
                while(tree[p].bro!=0)
                {
                    p=tree[p].bro;
                }
                tree[p].bro=i;
            }
            tree[i].len=l;
        }
        dfs(1);
        dfsAns(1,0);
        for(int i=1; i<=n; i++)
        {
            printf("%d\n",tree[i].ans);
        }
    }
    return 0;
}
/*
//题解中的例子的数据
9
1 1
1 2
3 3
2 2
3 4
3 1
6 2
6 3
*/


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值