【树形DP】 HDU 2196 Computer

题目链接:  HDU 2196 Computer

分析:   先从任意一点开始, 求出它到其它点的最大距离, 然后以该点为中心更新它的邻点,

           再用被更新的点去更新邻点......依此递推 !

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>

using namespace std;
const int inf = 0x7FFFFFFF;
const int maxn = 11111;

struct node{
    int to, dix, sum;
    node *next;
}tree[maxn<<1], *head[maxn];

int ptr, n; 
bool vis[maxn];
int dp[maxn],h[maxn];

void Init(){
    ptr=1;
    memset(dp,0,sizeof(dp));
    memset(vis,false,sizeof(vis));
    memset(head,0,sizeof(head));
}

void AddEdge(int x,int y,int s){
    tree[ptr].dix=s;
    tree[ptr].to=y;
    tree[ptr].next=head[x];
    head[x]=&tree[ptr++];
}

void DFS(int cnt){  
    vis[cnt]=true;
    node *p=head[cnt];
    while(p!=NULL){
        if(vis[p->to]) {
            p=p->next; continue;
        }
        DFS(p->to);
        dp[cnt]=max(dp[cnt],dp[p->to]+p->dix);
        p->sum=dp[p->to]+p->dix;
        p=p->next;
    }
}

void Tree_Dp(int father, int son){
    if(vis[son]) return ;
    vis[son]=true;
    int Max=0;
    node* p=head[father];
    while(p!=NULL){
        if(p->to!=son) 
            Max=max(Max,p->sum);
        p=p->next;
    }
    p=head[son];
    while(p!=NULL){
        if(p->to==father){
            p->sum=p->dix+Max;  break;
        }
        p=p->next;
    }
    p=head[son];
    while(p!=NULL){
        dp[son]=max(dp[son],p->sum);
        Tree_Dp(son,p->to);
        p=p->next;
    }
}
int main(){
    while(~scanf("%d",&n)&&n){
        Init();
        for(int i=2;i<=n;++i){
            int a,b;  scanf("%d%d",&a,&b);
            AddEdge(a,i,b);
            AddEdge(i,a,b);
        }
        
        DFS(1);          ///得到1点到其它所有点的距离
        
        memset(vis,false,sizeof(vis));
        node* p=head[1];
        while(p!=NULL){  ///从1的邻点开始更新
            Tree_Dp(1,p->to);
            p=p->next;
        }
        for(int i=1;i<=n;++i)
            printf("%d\n",dp[i]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值