CODEVS5565 two-prong apple tree

Source: Internet
Author: User

The root of the n<=100 point is a 1-two-tree with apples on the edge of the tree, and the maximum number of apples on the q<=n edge is preserved.

Tree dp,f[i][j]--node I is the subtree of the root preserving J-Edge optimal scheme, F[i][0]=0,f[i][j]=max (F[lc[i]][k-1]+f[rc[i]][j-k-1]+v[lc[i]]+v[rc[i]), This is left and right to choose the case, and then only select the left-only option.

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <algorithm>5 //#include <iostream>6 using namespacestd;7 8 intn,q;9 #defineMAXN 111Ten intF[MAXN][MAXN]; One structedge{intto,next,v;}; A structTree - { -Edge edge[maxn<<1]; the     intLC[MAXN],RC[MAXN],V[MAXN]; -     intFirst[maxn],le; -Tree () {memset (First,0,sizeof(first)); le=2;} -     void inch(intXintYintv) +     { -edge[le].to=y; +edge[le].v=v; Aedge[le].next=First[x]; atfirst[x]=le++; -     } -     voidInsertintXintYintv) -     { -         inch(x,y,v); -         inch(y,x,v); in     } -     voidDfsintXintFA) to     { +lc[x]=rc[x]=0; -          for(intI=first[x];i;i=edge[i].next) the             if(edge[i].to!=FA) *             { $                 if(!lc[x]) lc[x]=edge[i].to;Panax Notoginseng                 Elserc[x]=edge[i].to; -v[edge[i].to]=edge[i].v; the DFS (edge[i].to,x); +             } A     } the     voiddpintx) +     { -         if(Lc[x]) DP (lc[x]); $         if(Rc[x]) DP (rc[x]); $f[x][0]=0; -          for(intI=1; i<=q;i++) -         { theF[x][i]=max (f[lc[x]][0]+f[rc[x]][i-1]+v[rc[x]],f[lc[x]][i-1]+f[rc[x]][0]+V[lc[x]]); -              for(intj=1; j<=i-1; j + +)WuyiF[x][i]=max (f[x][i],f[lc[x]][j-1]+f[rc[x]][i-j-1]+v[lc[x]]+V[rc[x]]); the         } -     } Wu }t; - intx,y,v; About intMain () $ { -scanf"%d%d",&n,&Q); -      for(intI=1; i<n;i++) -     { Ascanf"%d%d%d",&x,&y,&v); + T.insert (x,y,v); the     } -T.dfs (1,0); $T.DP (1); theprintf"%d\n", f[1][q]); the     return 0; the}
View Code

CODEVS5565 two-prong apple tree

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.