Apple Tree POJ-2486

Source: Internet
Author: User

Apple Tree POJ-2486

The main topic: a point with the right to have a root tree, the root node is 1. From the root node, take the K-step, to collect the maximum weight and.

Tree-shaped DP. The complexity may be O (metaphysics) and will not exceed $o (nk^2) $. (Anyway, this question does not card this, the examination thought) reference

Ans[i][j][0] means I point below a total of walk J step, do not come back, may collect the maximum weight value
Ans[i][j][1] means I point below a total of walk J step, come back, may collect the maximum weight value

It is more complicated to use a backpack when each node (hereinafter referred to as the current node) is transferred from its child nodes:

T[i][j][0] Indicates that the front I child node of the current node is walking J step, not coming back
T[I][J][1] Indicates that the front I sub-node of the current node is walking J step back

For T[i][j][0], either the former I-1 sub-node of the current node takes J-step (including the number of steps to go and back to the child nodes in front of it), and does not come back before;

Either the former I-1 sub-node j-p step (including the number of steps to go and back to the front of the child node), the current node to go to the sub-node I 1 steps, I sub-node p-1 step down, do not come back;

Either take a step to the sub-node I, the sub-node I walk down p-2 step, then take a step back to the current node, and then in the former I-1 sub-node j-p step (including the number of steps to go and back to the front of the child node) and do not come back.

So T[i][j][0]=max (T[i-1][j][0],max{t[i-1][j-p][1]+ans[nowson][p-1][0]},max{t[i-1][j-p][0]+ans[nowson][p-2][1]})

For T[i][j][1], either the former I-1 sub-node j-p step (including the number of steps to go and back to the front of the child node), walk to the sub-node I took 1 steps, I sub-node walk down with P-2 step and back, from the sub-node I back to take a step Or the former I-1 sub-node takes a J-step (including the number of steps to go and back in front of the child nodes), back.

So T[i][j][1]=max (T[i-1][j][1],max{t[i-1][j-p][1]+ans[nowson][p-2][1]})

Of course, the actual solution does not require each node to open a T array, just need to do on the ANS array directly on the line. Is the first to solve the T array process with a scrolling array optimization, then only two D T[J][0/1]. Just take the array of ans[current node] as T to do it. Also, consider the bounds of the T array. In addition, the T array is initialized to the current node weight before it is solved.

The final answer is obvious: Max (Ans[1][k][0],ans[1][k][1]).

Once error:

Transfer equation of naive:

T[i][j][0]=max (T[i-1][j][0],t[i-1][j-p][0],t[i-1][j-p][1]+ans[son][p][0])
T[i][j][1]=max (T[i-1][j][1],t[i-1][j-p][1]+ans[son][p][1])

In fact, the 3rd (red) Case of the transfer of t[i][j][0] is easy to miss. In addition, it is easy to ignore the 1 or 2 steps that are spent walking and walking back to the child nodes.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 structEdge6 {7     intTo,next;8}edge[ About];9 intne,ans[ the][ About][2],f1[ the];Ten inta[ the]; One intn,k; A BOOLvis[ the]; - voidDfsintu) - { the     intj,kk=f1[u],p,v; -vis[u]=true; -      for(j=0; j<=k;j++) -ans[u][j][0]=ans[u][j][1]=A[u]; +      while(kk!=0) -     { +v=edge[kk].to; A         if(!Vis[v]) at         { - Dfs (v); -              for(j=k;j>=0; j--) -             { -                  for(p=1;p <=j;p++) -ans[u][j][0]=max (ans[u][j][0],max (ans[u][j-p][0]+ans[v][p-2][1],ans[u][j-p][1]+ans[v][p-1][0])); in                  for(p=2;p <=j;p++) -ans[u][j][1]=max (ans[u][j][1],ans[u][j-p][1]+ans[v][p-2][1]); to             } +         } -kk=Edge[kk].next; the     } * } $ intMain ()Panax Notoginseng { -     intI,TA,TB; the      while(SCANF ("%d%d", &n,&k) = =2) +     { ANe=0; thememset (ans,0,sizeof(ans)); +memset (Vis,0,sizeof(Vis)); -memset (F1,0,sizeof(F1)); $          for(i=1; i<=n;i++) $scanf"%d",&a[i]); -          for(i=1; i<n;i++) -         { thescanf"%d%d",&ta,&TB); -edge[++ne].to=TB;Wuyiedge[ne].next=F1[ta]; thef1[ta]=NE; -edge[++ne].to=ta; Wuedge[ne].next=F1[TB]; -f1[tb]=NE; About         } $Dfs1); -printf"%d\n", Max (ans[1][k][0],ans[1][k][1])); -     } -     return 0; A}

Apple Tree POJ-2486

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.