POJ2486 Apple Tree "backpack"

Source: Internet
Author: User

A word test instructions: a tree, a total of n points, each point has a weight, ask to start from 1, walk the K-step, up to the right value to traverse. can go back.

The first (second) road tree knapsack problem, first looked at the Dalao, changed a bit on the example. However.... TLE??? It's been a while. found that because of multiple sets of data and no "0 0" input, if not read in the time to add "~" or "EOF" will be dead loop, resulting in tle.

State design: Set F[I][J][0/1] As the root of the subtree, Walk J, can get the maximum weight (0/1 of the expression will be described in the transfer equation) consider: (here refer to [email protected], invasion and deletion of QWQ) each node in the final answer type: 1, do not pass; 2, passing but not returning; 3, passing and returning (the definition returned is that the final stop node is not in the subtree of that node) then it can be transferred:

Then there are some details: assigning an initial value to an array (starting with 0!) Walk 0 steps) to update the head array.

The implementation is still relatively simple.

Code

1#include <cstdio>2#include <algorithm>3#include <cstring>4 5 using namespacestd;6 7 intN,k,tot;8 intw[ -],head[ -];9 intf[ -][ $][3]; Ten structnode{ One     intTo,next,val; A}edge[ -]; -  - voidAddintXinty) the { -edge[++tot].to=y; -edge[tot].next=Head[x]; -head[x]=tot; + } -  + voidReadint&x) A { atx=0; -     CharCh=GetChar (); -     BOOLflag=false; -      while(ch<'0'|| Ch>'9') flag|= (ch=='-'), ch=GetChar (); -      while(ch>='0'&&ch<='9') x= (x<<3) + (x<<1) + (ch^ -), ch=GetChar (); -X=flag? -x:x; in } -  to voidTREEDP (intUintFA) + { -      for(intI=0; i<=k;i++) f[u][i][1]=w[u],f[u][i][0]=W[u]; the      for(intI=head[u];i;i=edge[i].next) *     { $         intv=edge[i].to;Panax Notoginseng         if(V==FA)Continue; - TREEDP (v,u); the          for(intkk=k;kk>=0; kk--) +          for(intj=0; j<=kk;j++) A         { the             if(kk>=j+2) f[u][kk][0]=max (f[u][kk][0],f[v][j][0]+f[u][kk-j-2][0]); +             if(kk>=j+2) f[u][kk][1]=max (f[u][kk][1],f[v][j][0]+f[u][kk-j-2][1]); -             if(kk>=j+1) f[u][kk][1]=max (f[u][kk][1],f[v][j][1]+f[u][kk-j-1][0]); $         } $     } - } -  the voidInit () - {WuyiMemset (F,0,sizeof(f)); theMemset (Head,0,sizeof(head)); -tot=0; Wu }  -  About intMain () $ { -      while(SCANF ("%d%d", &n,&k)! =EOF) -     { -          for(intI=1; i<=n;i++) read (W[i]); A          for(intI=1; i<=n-1; i++) +         { the             intx=0, y=0; - read (x), read (y); $ Add (x, y), add (y,x); the         } theTREEDP (1,-1); theprintf"%d\n", Max (f[1][k][0],f[1][k][1])); the init (); -     } in     return 0; the}
View Code

Summary: The classification discussion is often also an important breakthrough in solving the problem.

POJ2486 Apple 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.