hdu2196 Computer(树形dp)

Computer

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


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:它子树中的最长链
2:最长逆链(即通过父节点到达树的边缘走的最长链+它到父节点的权值)
其实就是分往上走和往下走两种情况
对于1很好求,关键是2

对于最长逆链,可以通过父节点的最长子链获得,但是会有这样一种情况,父亲节点的最长链经过该儿子,此时求得的最长链不满足题意,我们要求的是 不经过该点的最长逆链

此时就需要记录一下次大子链,通过比较     儿子的最大子链+它到父节点的权值==父节点的最大子链 判断父节点的最大子链是否经过该子节点
设dp[i][0]表示该节点的最长子链,dp[i][1]次长子链,dp[i][2]最长逆链
先用一个dfs求出dp[i][0]和dp[i][1]
然后再dfs求出dp[i][2]

下面主要给出dp[i][2]的转移方程
满足 红字标明条件 dp[son][2]=max(dp[fa][2],dp[fa][1])+e[i].w
不满足                      dp[son][2]=max(dp[fa][2],dp[fa][0])+e[i].w

此时会出现这种情况:一个父节点有两个相同长度的最大子链,而根据这个判断,父节点的最大子链经过其中的一条子链(具体不知道那一条),这时应该怎么处理?
其实不用考虑这种情况,因为在这种情况下,父节点的次大子链和最大子链相等,刷新最大值的时候无影响

#include<stdio.h>
#include<algorithm>
#include<string.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=10005;
int dp[N][4],first[N],tot;
struct node
{
    int v,w,next;
}e[N];
void add(int u,int v,int w)
{
    e[tot].v=v;
    e[tot].w=w;
    e[tot].next=first[u];
    first[u]=tot++;
}
void dfs(int u,int fa)
{
    for(int i=first[u];~i;i=e[i].next)
    {
        int v=e[i].v;
        if(v==fa)continue;
        dfs(v,u);
        if(dp[u][0]<dp[v][0]+e[i].w)
        {
            dp[u][1]=dp[u][0];
            dp[u][0]=dp[v][0]+e[i].w;
        }
        else if(dp[u][1]<dp[v][0]+e[i].w)//这里别忘了加,因为可能出现两条长度相同的子链
             dp[u][1]=dp[v][0]+e[i].w;

    }
}
void dfs2(int u,int fa)
{
    for(int i=first[u];~i;i=e[i].next)
    {
        int v=e[i].v;
        if(v==fa)continue;

        if(dp[u][0]==dp[v][0]+e[i].w)
            dp[v][2]=max(dp[u][2],dp[u][1])+e[i].w;
        else
            dp[v][2]=max(dp[u][2],dp[u][0])+e[i].w;

        dfs2(v,u);
    }
}
int main()
{
    int n,v,w;
    while(~scanf("%d",&n))
    {
        tot=0;
        mem(first,-1);
        mem(dp,0);
        for(int i=2;i<=n;i++)
        {
            scanf("%d%d",&v,&w);
            add(v,i,w);
        }
        dfs(1,-1);
        dfs2(1,-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、付费专栏及课程。

余额充值