HDU 2196 Computer(树形dp 求结点到叶子的最远距离)

Computer

Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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
 

题意:给出一棵树,求结点到叶子的最大权值

对于每个结点,如果每一次都暴力寻找最远距离,那么复杂度达到O(n^2),不可取
但是由于是一棵树,那么对于每个结点来说,最远距离要么是在子树中,要么是从父节点过来的最远距离
那么就有了状态转移的方式,我们进行两次dfs,第一次统计每个结点的子树最远距离和次远距离,
第二次从根节点出发更新每个结点到叶子结点的最远距离和次远距离,那么这样一来就统计了每个结点的最大距离
时间复杂度O(n)

#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");

using namespace std;

#define INF 0x3f3f3f3f
#define maxn 20004

int dp[maxn][2],id[maxn];
int fir[maxn],nex[maxn],u[maxn],v[maxn],w[maxn];
int e_max;

void init()
{
    memset(dp,0,sizeof dp);
    memset(fir,-1,sizeof fir);
    for(int i=0;i<maxn;i++) id[i]=i;
    e_max=0;
}

void add_edge(int s,int t,int c)
{
    int e=e_max++;
    u[e]=s;
    v[e]=t;
    w[e]=c;
    nex[e]=fir[s];
    fir[s]=e;
}

void dfs1(int k)
{
    for(int i=fir[k]; ~i; i=nex[i])
    {
        dfs1(v[i]);
        int e=v[i];
        if(dp[e][1]+w[i]>dp[k][1])
        {
            dp[k][0]=dp[k][1];
            dp[k][1]=dp[e][1]+w[i];
            id[k]=e;
        }
        else if(dp[e][1]+w[i]>dp[k][0])
        {
            dp[k][0]=dp[e][1]+w[i];
        }
    }
}

void dfs2(int k)
{
    for(int i=fir[k];~i;i=nex[i])
    {
        int e=v[i];
        if(id[k]==e)
        {
            if(dp[k][0]+w[i]>dp[e][1])
            {
                dp[e][0]=dp[e][1];
                dp[e][1]=dp[k][0]+w[i];
                id[e]=k;
            }
            else if(dp[k][0]+w[i]>dp[e][0])
            {
                dp[e][0]=dp[k][0]+w[i];
            }
        }
        else
        {
            if(dp[k][1]+w[i]>dp[e][1])
            {
                dp[e][0]=dp[e][1];
                dp[e][1]=dp[k][1]+w[i];
                id[e]=k;
            }
            else if(dp[k][1]+w[i]>dp[e][0])
            {
                dp[e][0]=dp[k][1]+w[i];
            }
        }
        dfs2(e);
    }
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(int i=2; i<=n; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            add_edge(a,i,b);
        }
        dfs1(1);
        dfs2(1);
        for(int i=1;i<=n;i++)
        {
            printf("%d\n",dp[i][1]);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值