标签:树形dp
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
5
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm> using namespace std; const double eps = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int MOD = 1000000007; #define ll long long #define CL(a,b) memset(a,b,sizeof(a)) struct Tree { int father;//父亲 int child;//儿子 int brother;//兄弟 int Not;//不去party int TakeParty;//去party int MAX() {return TakeParty > Not ? TakeParty : Not;}//结构体调用函数省时间 int init()//树清空 { father = child = brother = Not = 0; } }tree[6005]; void dfs(int idx) { int child = tree[idx].child; while(child)//如果有孩子继续查 { dfs(child); tree[idx].TakeParty += tree[child].Not;//如果idx去party,那么它的孩子就不能去 tree[idx].Not += tree[child].MAX();//如果idx不去party,那么它的孩子可去可不去 child = tree[child].brother;//查找其他孩子 } } int main() { int n,a,b; while(scanf("%d",&n)==1) { for(int i=1; i<=n; i++) { scanf("%d",&tree[i].TakeParty); tree[i].init(); } while(scanf("%d%d",&a,&b),a+b)//建树 { tree[a].father = b; tree[a].brother = tree[b].child; tree[b].child = a; } for(int i=1; i<=n; i++)//查找 { if(!tree[i].father) { dfs(i); printf("%d\n",tree[i].MAX()); break; } } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdu1520 Anniversary party(poj2342,树形dp)
标签:树形dp
原文地址:http://blog.csdn.net/d_x_d/article/details/49702445