Description
Wshxzt is a lovely girl. She likes Apple very much. One day HX takes her to a apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trips at one node. She can eat the apples in the nodes she reaches. HX is a kind guy. He knows this eating too many can make the lovely girl fat. So him doesn ' t allow wshxzt to go more than K steps in the tree. It costs one step while she goes from one node to another adjacent node. Wshxzt likes Apple very much. So she wants to eat as many as she can. Can tell how many apples she can eat in most K steps.
Input
There are several test cases in the input
Each test case contains three parts.
The "a" is two numbers N K, whose meanings we have about talked now. We denote the nodes by 1 2 ... N. Since It is a tree that each node can be reach the any and only one route. (1<=n<=100, 0<=k<=200)
The second part contains N integers (all integers are nonnegative and not bigger than 1000). The ith number is the amount of apples in Node I.
The third part contains N-1 line. There are two numbers a,b in the each line, meaning the node A and node B are adjacent.
Input is ended by the ' end of ' of file.
Note:wshxzt starts at Node 1.
Output
For each test case, output the maximal numbers of apples wshxzt can eat in a line.
Sample Input
2 1
0 1 2 3 2 0 1 2 1 2 1-
3
Sample Output
One
2
the
Give you an apple tree, each node has a corresponding Apple, to start from Node 1 to go up to K step, can eat the largest number of apples.
train of Thought
A tree DP, it's easy to think of dp[root][k] represents the maximum value that can be obtained by taking root as root and taking a maximum of k steps.
Then this K-step can be assigned to each of its subtree.
If the binary tree is still better to solve, but for this problem, we also want to introduce another state: to traverse the subtree after the root node
we define
DP[ROOT][K][1] represents the maximum value that can be obtained by taking root at a maximum of k steps and eventually returning to root.
Dp[root][k][0] represents the maximum value that takes root as a maximum of K-steps and ultimately does not return to root.
Then there is the state transition equation:
Dp[root][k][0]=max (Dp[root][k][0],dp[root][k-s][1]+dp[son][s-1][0]);
Dp[root][k][0]=max (dp[root][k][0],dp[root][k-s][0]+dp[son][s-2][1]);
Dp[root][k][1]=max (dp[root][k][1],dp[root][k-s][1]+dp[son][s-2][1]);
S is the number of steps we allocate for the currently judged child node, S-1 is a step from root to Son, s-2 from Root to son and then back from son to the two steps required by root.
AC Code
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std;
#define INF 0x3f3f3f3f typedef __int64 LL;
struct node {int u,v,next;} tree[510];
int head[210];
int cnt;
int n,k;
int dp[210][510][2];
int val[210];
void Add (int u,int v) {tree[cnt].u=u;
Tree[cnt].v=v;
Tree[cnt].next=head[u];
head[u]=cnt++;
} void Dfs (int root,int mark) {for (int i=head[root]; i!=-1; i=tree[i].next) {int son=tree[i].v;
if (Son==mark) continue;
DFS (Son,root); for (int j=k; j>=1; j--) {for (int s=1; s<=j; s++) {dp[root][j][0]=m
Ax (dp[root][j][0],dp[root][j-s][1]+dp[son][s-1][0]);
Dp[root][j][0]=max (dp[root][j][0],dp[root][j-s][0]+dp[son][s-2][1]);
Dp[root][j][1]=max (dp[root][j][1],dp[root][j-s][1]+dp[son][s-2][1]);
int main () {while (~scanf ("%d%d", &n,&k)) { Memset (Dp,0,sizeof (DP));
Memset (head) (head,-1,sizeof);
for (int i=1; i<=n; i++) {scanf ("%d", val+i);
for (int j=0; j<=k; j + +) Dp[i][j][0]=dp[i][j][1]=val[i];
} cnt=0;
for (int i=1; i<n; i++) {int a,b;
scanf ("%d%d", &a,&b);
Add (a,b);
Add (B,a);
DFS (1,0);
printf ("%d\n", Max (dp[1][k][0],dp[1][k][1));
return 0; }