POJ 2486 Apple trees (tree dp, tree backpack)

Source: Internet
Author: User

Test instructions: Given a tree map, a person from point s, can only walk the K-step, each point has a certain number of apples, asked to collect as many apples as possible, output the maximum number of apples.

Ideas:

Since it is a tree, and there are restrictions on the K-step, then the tree-shaped DP just.

Consider a situation where: (1) may be after the completion of Step K (2) at the end of the tree, and then go back to this node (step k is not in the book tree)

The second kind of simple, backpack, is to enumerate to this node's children t How many steps, collect up to the maximum number of apples. The first requires that step K be terminated in one of the sub-trees under this node, then only in 1 children's subtree, so it should be "other children all have to come back" + "the child does not ask to come back" or "some other child does not walk back" + "This node comes back."

Use two DP arrays to differentiate between "back" and "not back" on the line, note, "Do not return" only 1 children do not require it to walk back, "back" is all back. The number of apples collected "without asking for a return" must be greater than or equal to "request back".

1 //#include <bits/stdc++.h>2#include <cstdio>3#include <cstring>4#include <iostream>5 #definePII pair<int,int>6 #defineINF 0x3f3f3f3f3f3f3f3f7 #defineLL Long Long8 using namespacestd;9 Const intn= About;Ten structnode One { A     int  from, To,next; - node () {}; -Nodeint  from,intTo,intNext): from( from), to, next (next) {}; the}edge[n*2]; - intedge_cnt, Head[n], w[n]; - voidAdd_node (int  from,intto ) - { +Edge[edge_cnt]=node ( from, To, head[ from]); -head[ from]=edge_cnt++; + } A /* at Dp[][][1] Records are returned to this node each time.  - Dp[][][0] records only 1 times without returning to this node.  - */ - intdp[n][n][2]; - voidDFS (intTintFarintm) - { in node E; -      for(intj=0; j<=m; J + +) dp[t][j][0]=dp[t][j][1]=W[T];//Now that we can get here, at least take this node. to     if(m==0)return; +      for(intI=HEAD[T]; i!=-1; I=e.next) -     { theE=Edge[i]; *         if(e.to^Far ) $         {Panax NotoginsengDFS (e.to, T, M-1); -              for(intJ=m; J>0; j-- ) the             { +                  for(intk=0; K +2<=j; k++ ) A                 { the                     //all branches back.  +dp[t][j][1]=max (dp[t][j][1], dp[t][j-k-2][1]+dp[e.to][k][1] ); -                     //This branch is going back, but not back in the other branches. Because there are already 1 not back, so update in ' [0] '.  $dp[t][j][0]=max (dp[t][j][0], dp[t][j-k-2][0]+dp[e.to][k][1] ); $                 } -                  for(intk=0; K +1<=j; k++ ) -                 { the                     //not back, but the other branches must be all back.  -dp[t][j][0]=max (dp[t][j][0], dp[t][j-k-1][1]+dp[e.to][k][0] );Wuyi                 } the             } -         } Wu     } - } About  $  -  - intMain () - { A     //freopen ("Input.txt", "R", stdin); +     intN, K, A, b; the      while(~SCANF ("%d%d",&n,&K)) -     { $Edge_cnt=0; theMemset (Head,-1,sizeof(head)); the  the          for(intI=1; i<=n; i++) scanf ("%d",&w[i]); the          for(intI=1; i<n; i++) -         { inscanf"%d%d",&a,&b); the Add_node (A, b); the Add_node (b,a); About         } theDFS (1, -1, K); thecout<<dp[1][k][0]<<Endl; the     } +     return 0; -}
AC Code

POJ 2486 Apple trees (tree dp, tree 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.