POJ2486 Apple Tree

Source: Internet
Author: User

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld &%llu

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]

If you go to the end of the X, you have gone to the Y-step, in addition to the subtree, there is another choice: to return to the parent node, to the parent node of the other subtree.

Therefore, more than the normal tree-like DP one-dimensional state, the record has no return to the current node. 0 means back, 1 means no return.

Too lazy to write an analysis, copy a 233

Dp[root][j][0] = MAX (dp[root][j][0], dp[root][j-k][0] + dp[son][k-2][0]);//starting from S, to go back to s, need to walk two more steps s-t,t-s, assigned to the T subtree K step, Other subtrees j-k step, all returned

DP[ROOT][J]][1] = MAX (dp[root][j][1], dp[root][j-k][0] + dp[son][k-1][1]);//go through the other subtrees of S, go back to S, traverse the T subtree, do not return in the current subtree, take one more step

DP[ROOT][J][1] = MAX (dp[root][j][1], dp[root][j-k][1] + dp[son][k-2][0]);//Do not go back to s (other subtree to s), return in the T subtree, same as two more steps

By the dancer on the keyboard

I actually write when 0 and 1 are contrary to what is said above.

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 Const intmxn= -;9 structedge{Ten     intv; One     intNXT; A }E[MXN]; - inthd[mxn],mct=0; - voidAdd_edge (intUintv) { thee[++mct].v=v;e[mct].nxt=hd[u];hd[u]=MCT; -     return; - } - intf[mxn][mxn][2];//The third dimension 0 means that the root node is not returned, and 1 means that the root node is returned + intW[MXN]; - intn,m; + voiddpintUintFA) { A     inti,j,k; at      for(i=hd[u];i;i=e[i].nxt) { -         intv=e[i].v; -         if(V==FA)Continue; - DP (V,U); -          for(j=m;j;--j) { -              for(k=1; k<=j;++k) { inf[u][j][1]=max (f[u][j][1],f[u][j-k][1]+f[v][k-2][1]); -f[u][j][0]=max (f[u][j][0],f[u][j-k][1]+f[v][k-1][0]); tof[u][j][0]=max (f[u][j][0],f[u][j-k][0]+f[v][k-2][1]); +             } -         } the     } *     return; $ }Panax Notoginseng intMain () { -      while(SCANF ("%d%d", &n,&m)! =EOF) { theMemset (E,0,sizeofe); +memset (HD,0,sizeofHD); AMemset (F,0,sizeof 0); themct=0; +         inti,j; -          for(i=1; i<=n;i++){ $scanf"%d",&w[i]); $              for(j=0; j<=m;j++){ -f[i][j][0]=f[i][j][1]=W[i]; -             } the         } -         intu,v;Wuyi          for(i=1; i<n;i++){ thescanf"%d%d",&u,&v); - Add_edge (u,v); Wu Add_edge (v,u); -         } Aboutdp1,0); $printf"%d\n", f[1][m][0]); -     } -     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.