HDU 2196 Computer 树形dp

B - Computer
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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

求一颗树上离某点最远的距离

我们先求从改点往下走最远能走多远

然后再求从这点往上最远能走多远

因为会出现从这点往上又往下走

只保存最大距离的话会出现从该点出发又回到该点的情况

所以要保存某点向下的最大距离和次大距离

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d,&x,&y,&z)
#define rdl(x) scanf("%I64d,&x);
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define ull unsigned long long
#define maxn 10005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
#define eps 1e-8
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll mul(ll a,ll b,ll p){ll sum=0;for(;b;a=(a+a)%p,b>>=1)if(b&1)sum=(sum+a)%p;return sum;}
inline void Scan(int &x) {
      char c;while((c=getchar())<'0' || c>'9');x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
using namespace std;
struct tree{
    int to,next;
    int data;
}my[maxn];
int head[maxn],tot;
int dp[maxn][3];
void init(){memset(head,-1,sizeof(head));tot=0;memset(dp,0,sizeof(dp));}
void add(int u,int v,int data){
    my[tot].to=v;my[tot].next=head[u];
    my[tot].data=data;head[u]=tot++;
}
void fun1(int st){
    int biggest=0,bigger=0,tmp=0;
    for(int i=head[st];i!=-1;i=my[i].next){
        int v=my[i].to;
        fun1(v);
        tmp=dp[v][0]+my[i].data;
        if(biggest<=tmp){
            bigger=biggest;
            biggest=tmp;
        }else if(bigger<tmp)
            bigger=tmp;

    }
    dp[st][0]=biggest;
    dp[st][1]=bigger;
}
void fun2(int st){
    for(int i=head[st];i!=-1;i=my[i].next){
        int v=my[i].to;
        dp[v][2]=max(dp[st][2],dp[v][0]+my[i].data==dp[st][0]?dp[st][1]:dp[st][0])+my[i].data;
        fun2(v);
    }
}
int main(){
    int n,loop,cnt=1;
    while(~rd(n)){
        int v,data;
        init();
        FOR(i,2,n){
            rd2(v,data);
            add(v,i,data);
        }
        fun1(1);fun2(1);
        FOR(i,1,n)printf("%d\n",max(dp[i][0],dp[i][2]));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值