#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineMax (a>b)? a:bConst intNMax = the;intMap[nmax][nmax];intD[nmax][nmax];intVisit[nmax];structroom{intbugs, Brains;} Room[nmax];intN, M;voiddpintU//The dynamic programming here is equal to layered processing, one layer at a time. Recursive non-recursive mutual mates. {Visit[u]=1; intR = (Room[u].bugs + +) / -; for(inti = M; I >= R; I--) D[u][i]= Room[u].brains;//Initialize for(intv =1; v <= N; ++v) {if(Map[u][v] &&!Visit[v]) {DP (v); for(intj = M; J >= R; J--)//It is important to note that from large to small, this avoids interference from the node itself. The d here only needs to handle the other subtrees. { //If you want to use recursion here, from M to R, perform update processing of U. for(intK =1; K <= J-r; K + +) D[u][j]= Max (D[u][j], d[u][j-k] +D[v][k]); } } }}intMain () { while(SCANF ("%d%d", &n, &m)! =EOF) { if(N = =-1) Break; for(inti =1; I <= N; ++i) scanf ("%d%d", &room[i].bugs, &room[i].brains); memset (Map,0,sizeof(map)); for(inti =1; i < N; ++i) {intA, B; scanf ("%d%d", &a, &b); MAP[A][B]= Map[b][a] =1; } if(! M//if M is zero, special judgment is required, special data{printf ("0\n"); Continue; } memset (d,0,sizeof(d)); memset (Visit,0,sizeof(visit)); DP (1); printf ("%d\n", d[1][m]); } return 0;}
HDOJ1011 (Tree-type motion regulation)