Poj2486---apple Tree

Source: Internet
Author: User

Apple Tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7598 Accepted: 2548

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


Tree-shaped DP

Set Dp[i][j][0] Indicates the maximum number of apples that can be obtained by walking J-step and not returning to I at the end of the subtree under the root of I

DP[I][J][1] Indicates the maximum number of apples that can be obtained by walking J-step at the end of the subtree with I as the root


Set U as a sub-root, V for one of the sons node

DP[U][I][1] = max (dp[u][i][1], dp[v][j-2][1] + dp[u][i-j][1] + apple[v]);

Dp[u][i][0] = max (dp[u][i][0], dp[u][i-j][1] + dp[v][j-1][0] + apple[v]);

Dp[u][i][0] = max (dp[u][i][0], dp[u][i-j][0] + dp[v][j-2][1] + apple[v]);


/************************************************************************* > File Name:POJ2486.cpp > Author: ALex > Mail: [email protected] > Created time:2015 January 02 Friday 11:30 01 seconds ******************************* /#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;vector <int> edge[110];  int dp[110][220][2];int apple[110];int N, m;void dfs (int u, int fa) {int size = Edge[u].size (); for (int i = 0; i < size; ++i) {int v = edge[u][i];if (v = = FA) {continue;} DFS (v, u); for (int j = m; j >= 1;--j) {for (int k = 1; k <= J; ++k) {if (K >= 2) {dp[u][j][1] = max (dp[u][j][1], DP [U] [J-k] [1] + dp[v][k-2][1] + apple[v]);DP [u][j][0] = max (dp[u][j][0], dp[v][k-2][1] + dp[u][j-k][0] + apple[v]);}Dp[u][j][0] = max (dp[u][j][0], dp[u][j-k][1] + dp[v][k-1][0] + apple[v]);}}} int main () {int u, v;while (~scanf ("%d%d", &n, &m)) {memset (DP, 0, sizeof (DP)), for (int i = 1; I <= n; ++i) {Edge [I].clear (); scanf ("%d", &apple[i]);} for (int i = 1; I <= n-1; ++i) {scanf ("%d%d", &u, &v); Edge[u].push_back (v); Edge[v].push_back (u);} DFS (1,-1);p rintf ("%d\n", dp[1][m][0] + apple[1]);} return 0;}


Poj2486---apple Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.