The title means: an N-node apple tree, each node has a certain number of apples; Ask from 1 to go to the apple that can be late for K-step (each step can only go to adjacent nodes)
Solution:
The following basic conditions are included in the walk (the other methods can be combined by the following):
1: On the tree is not back on the forward;
2: Go back to the node you walked over to another node:
So for each node there is a flag gone and not come back? Or walked by and came back!
The other is the backpack is only the dependency of the 01 backpack!
dp[flag][i][j]//the subtree of the node I walk J Step flag=1 representative did not return to I,flag=0 representative back to I
Dp[0][s][i+2]=Max(DP[0][s][i+2],dp[0][s][j]+dp[0][tmp. to][i-j]///Through the new subtree back so to +2 DP[1][s][i+2]=Max(DP[1][s][i+2],dp[1][s][j]+dp[0][tmp. to][i-j[1][s][i+1]=max (Dp[ 1][s][i+1],dp[0][s][j]+dp[1 ][tmp.to][i-j< Span class= "Sh-symbol");//Through the original tree, enter the new number, +1
#include <cstdio>#include<algorithm>#include<cmath>#include<cstdlib>#include<cstring>using namespacestd;Const intmaxn= the;Const intinf=0x3fffffff;structedge{intTo,dis,pre; Edge (intto=0,intdis=0,intPre=0): To, dis (dis), pre (PRE) {}}; Edge EDGE[MAXN];intHead[maxn],pos;intdp[2][maxn][205];intNUM[MAXN];intn,k;voidInint () {memset (head,-1,sizeof(head)); POS=0;}voidAdd_edge (intSintTo,intdis) {Edge[pos]=Edge (To,dis,head[s]); Head[s]=pos++;}voidDfsintPaints) { for(intI=0; i<=k;i++) dp[0][s][i]=dp[1][s][i]=Num[s]; for(intw=head[s];~w;w=edge[w].pre) {Edge&tmp=Edge[w]; if(TMP.TO==PA)Continue; DFS (S,TMP.TO); for(inti=k;i>=0; i--) { for(intj=0; j<=i;j++) {dp[0][s][i+2]=max (dp[0][s][i+2],dp[0][s][j]+dp[0][tmp.to][i-J]); dp[1][s][i+2]=max (dp[1][s][i+2],dp[1][s][j]+dp[0][tmp.to][i-J]); dp[1][s][i+1]=max (dp[1][s][i+1],dp[0][s][j]+dp[1][tmp.to][i-J]); } } }}intMain () {intb; while(~SCANF ("%d%d",&n,&K)) {inint (); for(intI=1; i<=n;i++) scanf ("%d",&Num[i]); for(intI=2; i<=n;i++) {scanf ("%d%d",&a,&b); Add_edge (A, B,1); Add_edge (B,a,1); } DFS (-1,1); printf ("%d\n", dp[1][1][k]); } return 0;}
< Span class= "Sh-symbol" > < Span class= "Sh-number" > < Span class= "Sh-symbol" > &NBSP;