`
SwordAndFlame
  • 浏览: 6582 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

树型DP 父子兄弟结构 poj 2342 Anniversary party

 
阅读更多
#include <iostream>
#include <cstdio>
#include <cstring>
struct Tree{
    int father;
    int child;
    int brother;
    int NOT;
    int s;
    int Max(){
        return s > NOT ? s : NOT;
    }
    void Init(){
    father=child=brother=NOT=0;
    }
}tree[60001];
void dfs(int v)
{
    int child;
    child=tree[v].child;
    while(child){
        dfs(child);
        tree[v].s+=tree[child].NOT;
        tree[v].NOT+=tree[child].Max();
        child=tree[child].brother;
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        for(int i=1; i<=n; i++){
            scanf("%d",&tree[i].s);
            tree[i].Init();
        }
        int f,c;
        while(scanf("%d%d",&c,&f)&&f+c){
            tree[c].father=f;
            tree[c].brother=tree[f].child;
            tree[f].child=c;
        }
        for(int i=1;i<=n;i++){
            if(!tree[i].father){
                dfs(i);
                printf("%d\n",tree[i].Max());
            }
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics