It was a very water tree-shaped DP question.
Set Dp[i][j], with J-man to attack the maximum benefit of a subtree with node I as its root
It turned out to be WA all night.
Reason:
Pit point 1:
Even if there is no guard in this node, if you want to get the benefit of this node, you still have to send a person to this node, or who will help you to get the proceeds?
Pit Point 2:
The topic is from Node 1 attack, and then I thought that the data given is based on 1 for the root node, there is no two-way edge
But
Finally I added two-way side, then WA.
After a long time, we found that the array of the forward stars was small.
Note: The array is small and may return wa,tle,re, etc.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 Const intmaxn= the;8 9 intDP[MAXN][MAXN];Ten intBUG[MAXN]; One intVAL[MAXN]; A - structEdge - { the intTo,next; - }; -Edge edge[maxn<<1]; - intHEAD[MAXN]; + inttot,n,m; - + voidInit () A { atmemset (head,-1,sizeofhead); -tot=0; - } - - voidAddedge (intUintv) - { inedge[tot].to=v; -edge[tot].next=Head[u]; tohead[u]=tot++; + } - the intsolve (); * $ intMain ()Panax Notoginseng { - while(SCANF ("%d%d",&n,&m)) { the if(n==-1&&m==-1) + Break; A init (); the for(intI=1; i<=n;i++){ + intu; -scanf"%d%d",&u,&val[i]); $Bug[i]= (u+ +)/ -; $ } - for(intI=1; i<n;i++){ - intu,v; thescanf"%d%d",&u,&v); - Addedge (u,v);Wuyi Addedge (v,u); the } - Wuprintf"%d\n", Solve ()); - } About return 0; $ } - - voidDfsintUintpre) - { A if(bug[u]>m) + return ; the for(inti=bug[u];i<=m;i++) -dp[u][i]=Val[u]; $ for(intI=head[u];~i;i=Edge[i].next) { the intv=edge[i].to; the if(v==pre) the Continue; the DFS (v,u); - for(intj=m;j>=bug[u];j--){ in for(intk=1; k<=j-bug[u];k++){ theDp[u][j]=max (dp[u][j],dp[u][j-k]+dp[v][k]); the } About } the } the } the + intSolve () - { the if(m==0)Bayi return 0; theMemset (DP,0,sizeofDP); theDfs1,-1); - - returndp[1][m]; the}
HDU 1011 Starship Troopers tree DP with pit point