Obviously a tree-shaped backpack, note the case of M 0.
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 6 Const intN =101;7 intHead[n];8 intBn[n];9 intValue[n];Ten intDp[n][n]; One intN, M, E; A - structEdge - { the intV, next; -} edge[n <<1]; - - voidAddedge (intUintv) + { -EDGE[E].V =v; +Edge[e].next =Head[u]; AHead[u] = e++; at } - - voidDfsintUintFA) - { - intTMP = (Bn[u] + +) / -; - for(inti = tmp; I <= m; i++) Dp[u][i] =Value[u]; in for(inti = Head[u]; I! =-1; i =edge[i].next) - { to intv =edge[i].v; + if(v = = FA)Continue; - Dfs (v, u); the for(intj = m; J >= tmp; j-- ) * { $ for(intK =1; J-k >= tmp; k++ )Panax Notoginseng { -DP[U][J] = max (Dp[u][j], dp[u][j-k] +dp[v][k]); the } + } A } the } + - intMain () $ { $ while(SCANF ("%d%d", &n, &m)! =EOF) - { - if(n = =-1&& m = =-1) Break; the for(inti =1; I <= N; i++ ) - {Wuyiscanf"%d%d", BN + I, value +i); the } -E =0; WuMemset (Head,-1,sizeof(head)); - for(inti =1; I < n; i++ ) About { $ intu, v; -scanf"%d%d", &u, &v); - Addedge (U, v); - Addedge (V, u); A } + if( !m) the { -Puts"0"); $ Continue; the } theMemset (DP,0,sizeof(DP)); theDfs1, -1 ); theprintf"%d\n", dp[1][m]); - } in return 0; the}
HDU 1011 Tree-shaped backpack