Test instructions
Given a tree, each node has a weight, child nodes and parent nodes can not be selected at the same time, ask what is the maximum value of the last choice?
Ideas:
DP[I][1] = Select, Dp[i][0] indicates not selected
The state transition equation is:
DP[I][1]+=DP[J][0];
Dp[i][0]+=max (Dp[j][1],dp[j][0]);
AC Code:
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<queue>#include<cmath>#include<algorithm>using namespacestd;#defineN 6010intdp[n][2];vector<int>Son[n];BOOLVis[n];voidTREEDP (intp) {Vis[p]=1; for(intI=0; I<son[p].size (); i++) { intu=Son[p][i]; if(!Vis[u]) TREEDP (U); dp[p][1]=dp[p][1]+dp[u][0]; dp[p][0]=dp[p][0]+max (dp[u][0],dp[u][1]); }}intMain () {//freopen ("In.txt", "R", stdin); intn,p,q; while(~SCANF ("%d",&N)) {memset (DP,0,sizeof(DP)); for(intI=1; i<=n; i++) {scanf ("%d", &dp[i][1]); Son[i].clear (); } memset (Vis,0,sizeof(VIS)); while(SCANF ("%d%d", &p,&q) && (p+q) {son[q].push_back (P); VIS[P]=1; } intRoot; for(intI=1; i<=n; i++) if(!Vis[i]) {Root=i; Break; } memset (Vis,0,sizeof(VIS)); TREEDP (root); printf ("%d\n", Max (dp[root][0],dp[root][1])); } return 0;}
HDU1520 Anniversary Party (tree DP entry)