This is a typical tree-like DP, And I have understood it for a long time, I feel good about myself .........................
So make it clear and remember it later.
A tree with n vertices (1-n) and n-1 edges. Each vertex has a weight value. Start from 1 and take step V, the maximum number of retrieved Weights
We use the thought of a backpack here. The number of steps is equivalent to the size of the backpack, and the weight on the page is equivalent to the value. Given a certain amount of backpack capacity, we can find the maximum value that can be loaded into the backpack.
Set dp [0] [s] [j] to start from s (current root node) and go to step j to return to the maximum weight that s can obtain.
Dp [1] [s] [j] indicates starting from s (current root node) and taking STEP j without returning to the maximum weight that s can obtain
Now we can allocate the backpack capacity: the parent node and the child node allocate the backpack capacity to design the state transition equation.
Main ideas:
S return, t return
S does not return, t returns (to the t subtree, then the t subtree returns to the other subtree of s, and then does not return to the s)
S return, t does not return (after traversing other child trees of s, return s, then go to the Child tree of t, and then do not return to t)
If none of them are returned, one party must have a return process and then go to the Child tree on the other side.
To sum up one sentence, either go to the other subtree of s, or go to the subtree of t, or return to the vertex of s.
1. Return in the t subtree, and return to the current root node s.
2. The root node is not returned, but is returned in the t subtree, that is, it is equivalent to taking the k step from t to return the optimal value of t plus the j-k step from s to the optimal value not returned by other subtree, and s is connected with t in the middle, in fact, it is equivalent to traversing the t subtree from s (dp [0] [t] [k]) and returning to s (this step has two more steps in the middle ), go out again (Other subtree) [dp [1] [s] [j-k], don't come back
3. The root node is not returned, and the t subtree is not returned. It is equivalent to traversing other subtree from s, return to s (dp [0] [s] [j-k]), go to the t subtree, and do not return to t (dp [1] [t] [k]), this process takes only one step.
Dp [0] [s] [j + 2] = Max (dp [0] [s] [j + 2], dp [0] [t] [k] + dp [0] [s] [j-k]); // starts from s and returns to s, take two more steps s-t, t-s, and assign them to the t subtree k step. All the other j-k steps are returned.
Dp [1] [s] [j + 2] = Max (dp [1] [s] [j + 2], dp [0] [t] [k] + dp [1] [s] [j-k]); // do not return to s (go to other subtree of s ), two more steps are returned in the t subtree.
Dp [1] [s] [j + 1] = Max (dp [1] [s] [j + 1], dp [1] [t] [k] + dp [0] [s] [j-k]); // first traverse other child trees of s and return to s, traverse the t sub-tree. If the current sub-tree t does not return, take one more step.
View Code
# Include <cstdio>
# Include <cstring>
# Include <vector>
Using namespace std;
Vector <int> g [1010];
Int dp [2] [1010] [210];
Int n, V, val [1010];
Bool used [1010];
Void Max (int & a, int B ){
If (a <B) a = B;
}
Void dfs (int s ){
Int I;
Used [s] = true;
For (I = 0; I <= V; I ++ ){
Dp [0] [s] [I] = dp [1] [s] [I] = val [s]; // force join
}
For (I = 0; I <g [s]. size (); I ++ ){
Int t = g [s] [I];
If (used [t]) continue;
Dfs (t );
For (int j = V; j> = 0; j --){
For (int k = 0; k <= j; k ++) {// The intermediate bridge must go, so the state is changed in turn
Max (dp [0] [s] [j + 2], dp [0] [t] [k] + dp [0] [s] [j-k]);
Max (dp [1] [s] [j + 2], dp [0] [t] [k] + dp [1] [s] [j-k]);
Max (dp [1] [s] [j + 1], dp [1] [t] [k] + dp [0] [s] [j-k]);
}
}
}
}
Int main (){
Int I, a, B;
While (scanf ("% d", & n, & V )! = EOF ){
For (I = 0; I <= 1000; I ++)
G [I]. clear ();
For (I = 1; I <= n; I ++)
Scanf ("% d", & val [I]);
For (I = 0; I <n-1; I ++ ){
Scanf ("% d", & a, & B );
G [a]. push_back (B );
G [B]. push_back ();
}
Memset (dp, 0, sizeof (dp ));
Memset (used, false, sizeof (used ));
Dfs (1 );
Printf ("% d \ n", dp [1] [1] [V]);
}
Return 0;
}