Luogu P2015 two fork apple tree

Source: Internet
Author: User

P2015 two fork Apple tree title description

There is an apple tree, if the branch has a fork, it must be divided into 2 forks (that is, there is no only 1 sons of the knot)

The tree has a total of N nodes (leaf Point or Branch fork Point), numbered 1-n, the root number must be 1.

We describe the position of a branch with the number of nodes connected at each end of a branch. Here's a tree with 4 branches.

2 5

\     /

3 4

\ /

1

Now there are too many branches to prune. But some branches have apples on them.

Given the number of branches that need to be retained, find out how many apples you can keep.

Input/output format

Input format:

The 1th line is 2 digits, N and Q (1<=q<= n,1<n<=100).

n represents the number of nodes in the tree, and Q indicates how many branches to keep. Next, the N-1 line describes the information of the branch.

3 integers per line, the first two being the number of nodes to which it is connected. The 3rd number is the number of apples on this branch.

There are no more than 30,000 apples on each branch.

Output format:

A number, the maximum number of apples that can be retained.

Input and Output Sample input example # #:
5 21 3 11 4 102 3 203 5 20
Sample # # of output:
21st

The knapsack problem on the tree, it is easy to think that if you delete a child node, then the subtrees tree of this node will be all deleted, then the subtree deleted by the node can not exceed the number of child nodes.
So we first DFS one side, find out how many nodes have how many subnodes, and mark Father, guarantee not to Father
f[i][j]< Delete the maximum of J edges in the I subtree, then the greedy maintenance is the biggest.
#include <iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#include<queue>#definell Long Longusing namespacestd;Const intmaxn= Max;intRead () {intan=0, f=1;CharCh=GetChar ();  while(! ('0'<=ch&&ch<='9')){if(ch=='-') f=-1; ch=GetChar ();}  while('0'<=ch&&ch<='9') {an=an*Ten+ch-'0'; ch=GetChar ();} returnf*an ;}intF[maxn],dp[maxn][maxn],cnt,fa[maxn],son[maxn],q,n;BOOLVIS[MAXN],VIS2[MAXN];structsaber{intTo,nex,wi;} B[MAXN<<1];voidAddintXintYintz) {CNT++; B[cnt].nex=F[x]; B[cnt].to=y; F[X]=CNT; B[cnt].wi=Z;}voidDfsintx) {Vis[x]=1; son[x]=1;  for(intI=f[x];i;i=B[i].nex) {        intv=b[i].to; if(!Vis[v])            {DFS (v); FA[V]=x; SON[X]+=Son[v]; }    }}voidDP (intx) {Vis2[x]=1;  for(intI=f[x];i;i=B[i].nex) {        intv=b[i].to; if(fa[x]!=v)            {DP (v);  for(intJ=min (SON[X],Q);j>0; j--)                 for(intK=min (J,Q);k>0; k--) Dp[x][j]=max (dp[x][j],dp[x][j-k]+dp[v][k-1]+b[i].wi); }    }}intMain () {n=read (); q=read ();  for(intI=1; i<n;i++){        intX=read (), Y=read (), z=read ();        Add (x, y, z);    Add (y,x,z); } DFS (1); DP (1); cout<<dp[1][q]; return 0;}
Apple_tree

By:s_a_b_e_r

Upstairs has been insisted two times DFS, in fact, it's good again ah Qwq

And since only down DP, the certificate of deposit to the side can be (but to save the father)

Because it's a two-prong tree.

If the sub-tree of point X is to keep the I root branch

Must be part (j) given to the left dial hand tree, the rest (i-j) to the right subtree

So you can happily fill in the form DP ^_^

#include <iostream>#include<cstdio>using namespacestd;Const intn=109;intN,q,p[n],fa[n],f[n][n],cnt,son[n];structedge{intTo,nex,val;} E[n<<1];voidAddintUintVintW) {     ++CNT; E[cnt].to=v; E[cnt].nex=P[u]; P[u]=CNT; E[cnt].val=W;}voidDfsintu) {Son[u]=1;  for(intI=p[u];i;i=E[i].nex) {       intv=e[i].to;       DFS (v); Son[u]+=Son[v];  for(intJ=min (Q,son[u]); j>=1;--j) for(intK=min (J,son[u]); k>=1;--k) F[u][j]=max (f[u][j],f[u][j-k]+f[v][k-1]+e[i].val); }}intMain () {scanf ("%d%d",&n,&q);  for(intI=1; i<n;++i) {intx, Y, Z scanf ("%d%d%d",&x,&y,&z); if(Fa[y]) {fa[x]=Y;add (y,x,z);} Else{fa[y]=x;add (x, y, z);} } DFS (1); cout<<f[1][q]<<Endl; return 0;}
Apple Apple tree

By:wypx

S:I has an apple ...

W:i has an another apple......apple tree!!!

Luogu P2015 two fork 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.