Apple Tree
Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 248664-bit integer IO format: %lld Java class name: Main
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.
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
2 1 0 11 1 2 3 2 0 1 2 1 2 1 3
Sample Output
11 2
Source
解题:树形dp
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 using namespace std; 6 const int maxn = 200; 7 vector<int>g[maxn]; 8 int dp[maxn][maxn][2],w[maxn],n,K; 9 void dfs(int u,int fa) { 10 for(int i = 0; i <= K; ++i) 11 dp[u][i][0] = dp[u][i][1] = w[u]; 12 for(int i = g[u].size()-1; i >= 0; --i) { 13 if(g[u][i] == fa) continue; 14 dfs(g[u][i],u); 15 for(int j = K; j > 0; --j) { 16 for(int k = 1; k <= j; ++k) { 17 dp[u][j][1] = max(dp[u][j][1],dp[u][j - k][0] + dp[g[u][i]][k - 1][1]); 18 if(k >= 2) { 19 dp[u][j][1] = max(dp[u][j][1],dp[u][j-k][1] + dp[g[u][i]][k-2][0]); 20 dp[u][j][0] = max(dp[u][j][0],dp[u][j - k][0] + dp[g[u][i]][k - 2][0]); 21 } 22 } 23 } 24 } 25 } 26 int main() { 27 int u,v; 28 while(~scanf("%d%d",&n,&K)) { 29 for(int i = 1; i <= n; ++i) scanf("%d",w+i); 30 for(int i = 0; i < maxn; ++i) g[i].clear(); 31 for(int i = 1; i < n; ++i) { 32 scanf("%d%d",&u,&v); 33 g[u].push_back(v); 34 g[v].push_back(u); 35 } 36 memset(dp,0,sizeof dp); 37 dfs(1,-1); 38 printf("%d ",max(dp[1][K][0],dp[1][K][1])); 39 } 40 return 0; 41 }