Poj2486apple tree[tree-shaped backpack!!!]

Source: Internet
Author: User

Apple Tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9989 Accepted: 3324

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] Test Instructions: Starting from 1, point right, ask K-Step Max . typical tree backpack, each point capacity K backpack per byte Point J is a group of K items per groupThe problem is, if you think of another child, go back to the rootD[I][J][0/1] Indicates subtree I walk J step back to root/not back to the maximum value of the roottransfer minute Three: D[i][j][0] one, d[i][j][1] Two: can be the current child does not come back, can also be the current child backSome details:1. Do not necessarily take the K-step, first put all the capacity of their own w[u]2. So don't use son, the capacity is K
////main.cpp//poj2486////Created by Candy on 9/27/16.//copyright©2016 Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intn=1e2+5, k=2e2+5, inf=1e9+5; inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx;}intn=0, k,u,v,w[n];structedge{intV,ne;} E[n<<1];intH[n],cnt=0; inlinevoidInsintUintv) {CNT++; E[CNT].V=v;e[cnt].ne=h[u];h[u]=CNT; CNT++; E[CNT].V=u;e[cnt].ne=h[v];h[v]=CNT;}intd[n][k][2];voiddpintUintFA) {     for(intI=0; i<=k;i++) d[u][i][0]=d[u][i][1]=W[u];  for(intI=h[u];i;i=e[i].ne) {        intv=e[i].v; if(V==FA)Continue;        DP (V,U);  for(intj=k;j>=1; j--){             for(intz=1; z<=j;z++){                if(z>=2) d[u][j][0]=max (d[u][j][0],d[u][j-z][0]+d[v][z-2][0]); if(z>=1) d[u][j][1]=max (d[u][j][1],d[u][j-z][0]+d[v][z-1][1]); if(z>=2) d[u][j][1]=max (d[u][j][1],d[u][j-z][1]+d[v][z-2][0]); }            //printf ("D%d%d%d%d\n", u,j,d[u][j][0],d[u][j][1]);        }    }}intMain () { while(SCANF ("%d%d", &n,&k)! =EOF) {CNT=0; Memset (H,0,sizeof(h));  for(intI=1; i<=n;i++) w[i]=read ();  for(intI=1; i<=n-1; i++) {u=read (); v=read (); Ins (u,v);} //memset (f,0,sizeof (f));dp1,-1); printf ("%d\n", d[1][k][1]); //cout<< "\ n"; //for (int i=1;i<=n;i++) printf ("Son%d%d\n", I,son[i]);    }}

Poj2486apple tree[tree-shaped backpack!!!]

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.