Test instructions: To a tree, each node has the right value, requires the selection of some nodes out, so that the sum of the weight of the largest, but each pair (father, son) can only pick one.
Ideas:
Compare the Getting Started topic, each node can be selected or not selected. If the current node is selected, the child must not select all, and if the current node is not selected, then the child can choose or not to choose.
1#include <bits/stdc++.h>2 #definePII pair<int,int>3 #defineINF 0x3f3f3f3f4 #defineLL Long Long5 using namespacestd;6 Const intn=7010;7 intW[n], dp[n][2], pre[n];8vector<int>Tree[n];9 Ten voidDasointt) One { A for(intI=0; I<tree[t].size (); i++) DFS (Tree[t][i]); - intsum[2]={0,0}; - for(intI=0; I<tree[t].size (); i++) the { -sum[0]+=max (dp[tree[t][i]][0], dp[tree[t][i]][1]);//Point T try not to take, then the child can be taken can also not take, take large person can. -sum[1]+=max (dp[tree[t][i]][0],0);//Point T try to fetch, then the child must not take all - } +dp[t][0]=sum[0]; -dp[t][1]=sum[1]+W[t]; + } A at - intMain () - { - //freopen ("Input.txt", "R", stdin); - intN, a, b, root; - while(cin>>N) in { -memset (PRE,0,sizeof(pre)); toMemset (DP,0,sizeof(DP)); + for(intI=0; i<=n; i++) tree[i].clear (); - the for(intI=1; i<=n; i++) scanf ("%d",&w[i]); * while(SCANF ("%d%d", &a,&b), A +b) $ {Panax Notoginseng Tree[b].push_back (a); -pre[a]=b; the } + for(intI=1; i<=n; i++)if(pre[i]==0) root=i; A DFS (root); theprintf"%d\n", Max (dp[root][0],dp[root][1]) ); + } - return 0; $}
AC Code
HDU 1520 Anniversary Party (tree DP, Getting started)