"POJ 2486" Apple Tree (DP)

Source: Internet
Author: User

Apple Tree

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

Test instructions
A tree, n points (1-n), n-1, each point has a weight, to start from 1, take the V-step, up to the right value to traverse.

Analysis
It's been a long time since I was too stupid to write a blog.
  

24 Hours of mental retardation every day. At first, I was looking at the wrong range, drunk.
This feeling is very dependent, so I first hit the DFS sequence that tree-dependent, and then found that I will not merge.
So I went to play the sub-tree version, made a super long, the whole of a silly.
The first problem is that we can go back, so set f[i][j][0] to show that I walk on the sub-tree of point I do not go backwards to the maximum value of I, f[i][j][1] means to go backwards.
One point in the subtree does not go back can only have one, pay attention not to leak ah not heavy ah just fine.
F[x][j][1]=max (F[x][j][1],f[y][l][1]+f[x][j-l-1][1])
F[x][j][0]=max (F[x][j][0],f[y][l][0]+f[x][j-l][1])
F[x][j][0]=max (F[x][j][0],f[y][l][1]+f[x][j-l-1][0])

One place is a subtree assuming that the size is size[x], then the number of steps in this subtrees tree will not exceed 2*size[x], because each edge up to two times (walk down), so can be to the size of the subtree twice times, this can be optimized to n^2.

Feel with the front of the dependency type or a bit different, the DFS sequence version can not be solved, it seems, because AH come back not to come back that good difficult to engage

The code is as follows:

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <queue>7#include <cmath>8 using namespacestd;9 #defineINF 0XFFFFFFFTen #defineMAXN 210 One  A structnode - { -     intX,y,next; the}t[2*MAXN];intLen; -  - intn,k; - intFIRST[MAXN],A[MAXN]; +  - intMymax (intXintY) {returnX>y?x:y;} +  A voidInsintXinty) at { -t[++len].x=x;t[len].y=y; -t[len].next=first[x];first[x]=Len; - } -  - intf[maxn][maxn][2],ans; in  - voidFfind (intXintFA) to { +f[x][1][0]=f[x][1][1]=A[x]; -      for(intI=first[x];i;i=t[i].next)if(t[i].y!=FA) the     { *         inty=t[i].y; $ Ffind (y,x);Panax Notoginseng          for(intj=k;j>=1; j--) -         { the              for(intL=1; l<=j;l++) +             { Af[x][j][1]=mymax (f[x][j][1],f[y][l][1]+f[x][j-l-1][1]),//l+1 thef[x][j][0]=mymax (f[x][j][0],f[y][l][0]+f[x][j-l][1]), +f[x][j][0]=mymax (f[x][j][0],f[y][l][1]+f[x][j-l-1][0]); -                 //F[x][l+1][0]=mymax (f[x][l+1][0],f[y][l][0]); $             } $         } -     } - } the  - intMain ()Wuyi { the      while(SCANF ("%d%d", &n,&k)! =EOF) -     { Wuans=-INF; -k++; About          for(intI=1; i<=n;i++) scanf ("%d",&a[i]); $len=0; -memset (First,0,sizeof(first)); -          for(intI=1; i<n;i++) -         { A             intx, y; +scanf"%d%d",&x,&y); the ins (x, y); ins (y,x); -         } $memset (f,- the,sizeof(f)); theFfind (1,0); the          for(intI=1; i<=k;i++) Ans=mymax (Ans,mymax (f[1][i][0],f[1][i][1])); theprintf"%d\n", ans); the     } -     return 0; in}
[POJ 2486]

2016-10-15 11:19:19



"POJ 2486" Apple Tree (DP)

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.