This is similar to poj1947. It is also a tree-like DP, because it is written according to the above ideas, which makes it very painful to write. after several hours of adjustment, it was still fruitless. it proves that the meaning of the status is not fully understood.
Setting DP [x] [0] [J] indicates taking node X as the root and taking step j at most and returning the maximum number of apples that can be obtained from X.
DP [x] [1] [J] indicates taking node X as the root and taking step j at most. You do not need to return the maximum number of apples that can be obtained from X.
Then we can get the dynamic transition equation (DP [x] [...] [J] is not complete yet, so we need to deal with it later ):
DP [x] [0] [J] = max (DP '[x] [0] [J-M] + dp [v] [0] [m]); 0 <= m <= J. DP indicates that the calculation ends with the previous child node, and V indicates the child node of X.
DP [x] [1] [J] = max (DP '[x] [1] [J-M] + dp [v] [0] [m], DP '[x] [0] [J-M] + dp [v] [1] [m]) 0 <= m <= J
In this case, DP [x] [...] [J] It does not calculate the weight of the node itself, and there is also a distance overhead to enter the node. Therefore, the program has a process of moving and updating the State during implementation.
The Code is as follows:
# Include <cstdlib> # include <cstring> # include <cstdio> # include <algorithm> # include <iostream> using namespace STD;/* meaning: given a tree full of apples, it tells us how many apples there are on each node. Now, when someone sends a hair from the node 1 and takes a maximum of K steps, maximum number of apples. solution: */struct node {int V, next;} e [105]; int N, S, G [105] [105], Apple [105], vis [105]; int head [105], idx, CH [105], DP [105] [2] [205]; void insert (int A, int B) {++ idx; E [idx]. V = B, E [idx]. next = head [a]; head [a] = idx;} V Oid visit (int x) {vis [x] = 1; for (INT I = 1; I <= N; ++ I) {If (! Vis [I] & G [x] [I]) {insert (X, I); ++ ch [X]; visit (I );}}} void DFS (int x) {If (head [x] =-1) {// If the leaf node return ;} int temp [2] [205] = {0}, V; For (INT I = head [X]; I! =-1; I = E [I]. next) {DFS (V = E [I]. v); For (Int J = s; j> = 2; j --) {DP [v] [0] [J] = DP [v] [0] [J-2] + apple [v];} DP [v] [0] [1] = DP [v] [0] [0] = DP [v] [1] [0] = 0; for (Int J = s; j> = 1; j --) {DP [v] [1] [J] = DP [v] [1] [J-1] + apple [v];} For (Int J = 0; j <= s; ++ J) {for (INT m = 0; m <= J; ++ m) {temp [0] [J] = max (temp [0] [J], DP [x] [0] [J-M] + dp [v] [0] [m]); temp [1] [J] = max (temp [1] [J], DP [x] [0] [J-M] + dp [v] [1] [m]); temp [1] [J] = max (temp [1] [J], DP [x] [1] [J-M] + dp [v] [0] [m]);} memcpy (DP [X], temp, sizeof (temp) ;}} int main () {int X, Y; while (scanf ("% d", & N, & S) = 2) {idx =-1; memset (DP, 0, sizeof (DP); memset (Head, 0xff, sizeof (head); memset (G, 0, sizeof (g); memset (CH, 0, sizeof (CH); memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= N; ++ I) {scanf ("% d", apple + I) ;}for (INT I = 1; I <n; ++ I) {scanf ("% d", & X, & Y); G [x] [Y] = G [y] [x] = 1 ;} visit (1); // create a DFS (1); printf ("% d \ n ", DP [1] [1] [s] + apple [1]);} return 0 ;}