Apple Tree
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 7784 |
|
Accepted: 2603 |
Description
Wshxzt is a lovely girl. She likes Apple very much. One day HX takes she to an apple tree. There is N nodes in the tree. Each node has a amount of apples. Wshxzt starts her happy trips 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 many apples she can eat in to the most K steps.
Input
There is several test cases in the input
Each test case contains three parts.
The first part is a numbers N K, whose meanings we have a talked about just now. We denote the nodes by 1 2 ... N. Since It is a tree with each node can reach any and other on only one route. (1<=n<=100, 0<=k<=200)
The second part contains N integers (all integers is 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 is numbers a, A, a, and node B are adjacent, meaning.
Input would 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 111 23 20 1 21 21 3
Sample Output
112
Source
poj contest,author:[email protected]
Topic Link: http://poj.org/problem?id= 2486
Topic: There is an n-node apple tree, each node has some apples, now from Node 1 to walk M step, ask the maximum number of apples can be eaten, a knot of apples are eaten up without the
Topic Analysis: The weak slag did a long time ah, test instructions very good understanding, The key is to be able to turn back, so we have to consider looking back when the state, do not look back two, that is,
Dp[u][i][0] from the node U walk I step last not back to 0 o'clock eat apple number
Dp[u][i][1] Represents from the node U go away I step last back to 0 o'clock eat apples number
Three kinds of cases:
U return v return: U to another subtree back to u and v subtree, traverse the V subtree, go back to V and then u, u, V, V, U more than 2 steps
U return v do not return: Span style= "font-size:14px" >u to another subtree back to u and to v subtree, and then down through the V subtree U-V 1 Step more The
U does not return v return: U traverse the v subtree and then go back to V and then back to u again to traverse other subtrees of U u-V, v-u 2 Step
State transition equation:
dp[u][j + 2][1] = max (dp[u) [j + 2] [1], dp[u][k][1] + dp[v][j-k][1])
Dp[u][j + 1][0] = max (dp[u][j + 1][0], dp[u][k][1] + dp[v][j-k][0])
Dp[u][j + 2][0] = max (dp[u][j + 2][0], dp[u][k][0] + dp[v][j-k][1])
Vector:
#include <cstdio> #include <cstring> #include <vector> #include <algorithm>using namespace std; int const MAX = 205;vector <int> t[max];int dp[max][max][2], val[max];int N, m;bool vis[max];void DFS (int u) {Vis [u] = true; for (int i = 0; I <= m; i++) dp[u][i][0] = dp[u][i][1] = Val[u]; Starting from u can obtain at least val[u] int len = T[u].size (); for (int i = 0; i < len; i++) {int v = t[u][i]; if (!vis[v]) {DFS (v); for (int j = m, j >= 0; j--) {for (int k = 0; k <= J; k++) { Dp[u][j + 2][1] = max (dp[u][j + 2][1], dp[u][k][1] + dp[v][j-k][1]); Dp[u][j + 1][0] = max (dp[u][j + 1][0], dp[u][k][1] + dp[v][j-k][0]); Dp[u][j + 2][0] = max (dp[u][j + 2][0], dp[u][k][0] + dp[v][j-k][1]); }}}}}int main () {while (scanf ("%d%d", &n, &m)! = EOF) {memset (dP, 0, sizeof (DP)); Memset (Vis, false, sizeof (VIS)); for (int i = 1; I <= n; i++) {scanf ("%d", &val[i]); T[i].clear (); } for (int i = 1; i < n; i++) {int u, v; scanf ("%d%d", &u, &v); T[u].push_back (v); T[v].push_back (U); } DFS (1); printf ("%d\n", dp[1][m][0]); }}
Array Tree:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const MAX = 205;int dp[ MAX][MAX][2], Val[max];int Head[max], cnt;int N, m;struct edge{int to, next; E[max * max/2];void Add (int x, int y) {e[cnt].to = y; E[cnt].next = Head[x]; HEAD[X] = cnt++;} void DFS (int u, int fa) {for (int i = 0; I <= m; i++) dp[u][i][0] = dp[u][i][1] = Val[u]; for (int i = head[u]; i =-1; i = e[i].next) {int v = e[i].to; if (v! = FA) {DFS (V, u); for (int j = m, j >= 0; j--) {for (int k = 0; k <= J; k++) { Dp[u][j + 2][1] = max (dp[u][j + 2][1], dp[u][k][1] + dp[v][j-k][1]); Dp[u][j + 1][0] = max (dp[u][j + 1][0], dp[u][k][1] + dp[v][j-k][0]); Dp[u][j + 2][0] = max (dp[u][j + 2][0], dp[u][k][0] + dp[v][j-k][1]); }}}}}int main () {while (scanf ("%d%d ", &n, &m)! = EOF) {cnt = 0; memset (DP, 0, sizeof (DP)); Memset (Head,-1, sizeof (head)); for (int i = 1; I <= n; i++) scanf ("%d", &val[i]); for (int i = 1; i < n; i++) {int u, v; scanf ("%d%d", &u, &v); ADD (U, v); Add (V, u); } DFS (1,-1); printf ("%d\n", dp[1][m][0]); }}
POJ 2486 Apple Tree (DP Classic)