hdu 2196 Computer(树形DP)

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[maxn][2],dp[i][0]表示i节点子树下到i节点最远距离,dp[i][1]表示(i的父亲节点的子树-i节点的子树)到i节点的最远距离。进行两次dfs。第一次dfs维护dp[i][0],第二次维护dp[i][1],在维护过程中记录最长,次长和最长路径的点。如果a节点在最长路径上,那么加上次长,否则加上最长。
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 ll;
const int inf=0x7f7f7f7f;
const int maxn=10005;
struct node
{
    int en,next,len;
} Edge[4*maxn];
int num,head[maxn];
ll dp[maxn][2];
bool vis[maxn];
void add(int st,int en,int len)
{
    Edge[num].en=en;
    Edge[num].len=len;
    Edge[num].next=head[st];
    head[st]=num++;
}
ll  dfs1(int u)
{
    vis[u]=true;
    for(int i=head[u]; i!=-1; i=Edge[i].next)
    {
        int v=Edge[i].en;
        if(vis[v]) continue;
        dp[u][0]=max(dp[u][0],dfs1(v)+Edge[i].len);
    }
    return dp[u][0];
}
void dfs2(int u)
{
    vis[u]=true;
    int max1=0,max2=0,v1,v2;
    for(int i=head[u]; i!=-1; i=Edge[i].next)
    {
        int v=Edge[i].en;
        if(vis[v]) continue;
        int temp=dp[v][0]+Edge[i].len;
        if(temp>max1)
        {
            max2=max1,v2=v1;
            max1=temp,v1=v;
        }
        else if(temp==max1||temp>max2)
        {
            max2=temp,v2=v;
        }
    }
    if(u!=1)
    {
        int temp=dp[u][1];
        if(temp>max1)
        {
            max2=max1,v2=v1;
            max1=temp,v1=-1;
        }
        else if(temp==max1||temp>max2)
        {
            max2=temp,v2=-1;
        }
    }
    for(int i=head[u]; i!=-1; i=Edge[i].next)
    {
        int v=Edge[i].en;
        if(vis[v]) continue;
        if(v==v1) dp[v][1]=max2+Edge[i].len;
        else dp[v][1]=max1+Edge[i].len;
        dfs2(v);
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        num=0;
        memset(head,-1,sizeof(head));
        memset(Edge,0,sizeof(Edge));
        int x,y;
        for(int i=2; i<=n; i++)
        {
            scanf("%d%d",&x,&y);
            add(i,x,y),add(x,i,y);
        }
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        dfs1(1);
        memset(vis,0,sizeof(vis));
        dfs2(1);
        for(int i=1; i<=n; i++)
            printf("%I64d\n",max(dp[i][0],dp[i][1]));
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值