树形dp,背包问题

Apple Tree

Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn’t allow Wshxzt to go more than K steps in the tree. It costs one step when 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 you tell how many apples she can eat in at most K steps.
Input
There are several test cases in the input
Each test case contains three parts.
The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 … N. Since it is a tree, each node can reach any other in 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 each line, meaning that Node A and Node B are adjacent.
Input will be ended by the end of file.

Note: Wshxzt starts at Node 1.
Output
For each test case, output the maximal numbers of apples Wshxzt can eat at a line.
Sample Input

1
2
3
4
5
6
7
2 1 
0 11
1 2
3 2
0 1 2
1 2
1 3

Sample Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
11
2
```
## 题意
给你一棵树,每个节点有果子,到这个节点可以全部拿走,从根节点出发,每次走到相邻的点,走K步,问能最多拿到多少果子。
## 题解
树形背包dp,显然的。<img src="https://jason-fxz.github.io/post-images/1580817296458.png" height="200" width="250"></img>

对于一个点u搜到其孩子点v ,会有三种情况:
1. 遍历v及其它孩子后回到u
2. 不回到u,遍历其他孩子回,v不回
3. 不回到u,遍历其他孩子不回(其中有一个不回),v回

故我们定义$dp[u][j][0]$表示要回到当前点u,走j步的最值,$dp[u][j][1]$表示不用回到当前点u,走j步的最值。
可得一下转移:

1. $$ dp[u][j+2][0]=max(dp[u][j+2][0],dp[u][j-k][0]+dp[v][k][0]) $$
2. $$ dp[u][j+1][1]=max(dp[u][j+1][1],dp[u][j-k][0]+dp[v][k][1]) $$
3. $$ dp[u][j+2][1]=max(dp[u][j+2][1],dp[u][j-k][1]+dp[v][k][0]) $$

j,k 分别枚举u,v背包的大小,进行合并。
1,3中 `j+2` 是因为u->v这条边要走两次
2中v不回,故只要走一次

## code
```c++
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=110;
const int K=205;
int dp[N][K][2];
int head[N],pnt[N<<1],nxt[N<<1],E;
int vl[N];
int n,m;
void checkmax(int &x,int y) {
if(y>x) x=y;
}
void add(int u,int v) {
E++;pnt[E]=v;nxt[E]=head[u];head[u]=E;
}
void init() {
memset(head,0,sizeof head);E=0;
memset(dp,0,sizeof dp);
}
void pt(int x) {
printf("dp:%d\n",x);
for(int i=0;i<=m;i++) {
printf("%d\t",dp[x][i][0]);
}printf("\n");
for(int i=0;i<=m;i++) {
printf("%d\t",dp[x][i][1]);
}printf("\n");
}
void dfs(int u,int f) {
for(int i=0;i<=m;i++)
dp[u][i][0]=dp[u][i][1]=vl[u];
// pt(u);
for(int i=head[u];i;i=nxt[i]) {
int v=pnt[i];
if(v==f) continue;
dfs(v,u);
for(int j=m;j>=0;j--) {
for(int k=0;k<=j;k++){
// 0 要回到当前点 1不用
checkmax(dp[u][j+2][0],dp[u][j-k][0]+dp[v][k][0]); // u回 v回,其他也回
checkmax(dp[u][j+2][1],dp[u][j-k][1]+dp[v][k][0]); // u不回 v回 其他不回
checkmax(dp[u][j+1][1],dp[u][j-k][0]+dp[v][k][1]); // u不回 v不回 其他回
}
}
}
}
int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
init();
for(int i=1;i<=n;i++) scanf("%d",&vl[i]);
for(int i=1;i<n;i++) {
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
dfs(1,0);
printf("%d\n",max(dp[1][m][1],dp[1][m][0]));
}
}