P2015 two-prong apple tree

Source: Internet
Author: User

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
Solution

I thought for a long time in the tree-shaped DP.

I thought it was the basic tree-like DP with the same routine as the elective course.

Then came up with a thought:

f [I][j] represents the subtree currently rooted at the current node, and the maximum value of the J-Edge is deleted.

And then I thought that the Benzi of each node and the sum of the fruits were first pretreated, and the results were found to be duly aftereffect ...

So, think of conventional thinking. However, in order to cancel the consideration of whether the current edge is a connecting edge of the root of the current subtree.

f [I][j] indicates that the current node must be preserved.

Then the state transfer is very simple, is a similar to the knapsack transfer equation.

But when I hit it, it was 0 before.

As it turns out, I've already initialized the f[x][1] and it's still being updated ...

Code
#include <bits/stdc++.h>using namespacestd;Const intmaxn= the;structsj{intto ; intNext; intW;} A[MAXN*2];intSIZE,HEAD[MAXN];intC[MAXN],N,M,NUM[MAXN];intF[MAXN][MAXN],V[MAXN];voidAddintXintYintz) {a[++size].to=y; A[size].next=Head[x]; HEAD[X]=size; A[SIZE].W=Z;}/*void Pre (int x) {for (int i=head[x];i;i=a[i].next) {int tt=a[i].to;            if (!c[tt]&&tt!=1) {pre (TT);        NUM[X]+=NUM[TT]; }    }}*/ //no eggs are initialized.voiddpintx) {V[x]=1;  for(intI=head[x];i;i=A[i].next) {        inttt=a[i].to; if(!V[tt]) {f[tt][1]=A[I].W;            DP (TT);  for(intj=m;j>=1; j--)             for(intk=0; k<=j;k++)            if((k!=j&&j!=1)|| x==1) F[x][j]=max (f[x][j],f[tt][k]+f[x][j-K]); }     }}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<n;i++)    {        intx, Y, Z scanf ("%d%d%d",&x,&y,&z);        Add (x, y, z);    Add (y,x,z); } Add (0,1,0); Pre (0); //memset (f,-1,sizeof (f));dp0); cout<<f[1][m]<<Endl;}

P2015 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.