Apple Tree
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 7789 |
|
Accepted: 2606 |
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
The main topic: give a tree of n nodes, each node has a value, ask not more than the highest possible value of K-step. I to J to calculate one step, j to I is also a step
Input: Enter n and K, then the value of N nodes, then n-1 i j for the I and J nodes are adjacent to each other. The root is 1.
It is easy to see that this is a tree-like dp,dp[i][j] represents the I node as the root, with J step can get the maximum value, but because go to the subtree is a step, go back to the root is a step, so there will be two DP relationship, Dp1[i][j] represents from the I node Walk J step back to the maximum value of J node, DP2[I][J] represents the maximum value of walking J step from the I node to the I node.
Then the state transition equation is: the current node is U, the subtree is V
Back to the I node: in the node U Walk J Step, in the subtree v Walk k step, from U to V and from V to U walk two steps, then in addition to the other sub-tree j-k-2 step away.
DP1[U][J] = max (Dp1[u][j],dp1[u][j-k-2]+dp1[v][k])
When not returning to the I node: Walk J Step from Node U
1. do not return u in the V subtree, then return u in the other subtree, walk K in V, step in U to V, walk j-k-1 step in the subtree except V.
DP2[U][J] = max (Dp2[u][j],dp1[u][j-k-1]+dp2[v][k])
2. return u in the V subtree, then there will be in the other subtree does not return u, in V Walk K, in the U to V and V to u walk two steps, in addition to the sub-tree outside the V Walk j-k-2 step.
DP2[U][J] = max (Dp2[u][j],dp2[u][j-k-2]+dp1[v][k])
Note 1: The input node i,j is an adjacent relationship, and the root is 1.
#include <cstdio> #include <cstring> #include <algorithm> #include <queue>using namespace std; struct tree{int V, Next;} Edge[110];int head[110], cnt; int dp1[110][210], dp2[110][210];//DP1 return, DP2 does not return DP[I][J]: maximum value int c[110] from I-node using J-Step, N , m; void Add (int u,int v) {edge[cnt].v = v; Edge[cnt].next = Head[u]; Head[u] = cnt++; return;} void Dfs (int u) {int I, j, K, V; Dp1[u][0] = dp2[u][0] = C[u]; if (head[u] = =-1) return; for (i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].v; DFS (v); } for (i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].v; for (j = m; J >= 0, j--) {for (k = 0; k <= J; k++) {if (k+2 <= j) { DP1[U][J] = max (dp1[u][j],dp1[u][j-k-2]+dp1[v][k]); DP2[U][J] = max (dp2[u][j],dp2[u][j-k-2]+dp1[v][k]); } if (k+1 <= j) Dp2[u][j]= Max (dp2[u][j],dp1[u][j-k-1]+dp2[v][k]); }}}}int map[110][110]; queue <int> que; void bfs (int n) {while (!que.empty ()) Que.pop (); Que.push (1); int i, u, v; while (!que.empty ()) {u = Que.front (); Que.pop (); for (i = 1; I <= n; i++) {if (map[u][i] = = 1) {Map[u][i] = Map[i][u] = 0; Add (u,i); Que.push (i); }}}}int Main () {int I, J, u, V; while (scanf ("%d%d", &n, &m)! = EOF) {memset (head,-1,sizeof (head)); memset (dp1,0,sizeof (DP1)); memset (dp2,0,sizeof (DP2)); memset (map,0,sizeof (MAP)); CNT = 0; for (i = 1; I <= n; i++) scanf ("%d", &c[i]); for (i = 1; i < n; i++) {scanf ("%d%d", &u, &v); MAP[U][V] = Map[v][u] = 1; } BFS (n); DFS (1); int max1 = 0; for (i = 0; I <=m; i++) {max1 = max (Max1,dp2[1][i]); } printf ("%d\n", max1); } return 0;}
Poj2486--apple Tree (DP)