读书人

树型DP 爷儿俩兄弟结构 poj 2342 Anni

发布时间: 2012-10-08 19:54:56 作者: rapoo

树型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());            }        }    }}

读书人网 >编程

热点推荐