Two-prong apple tree (Codevs 5565) tree DP

Source: Internet
Author: User

Title Description Description

There is an apple tree, if the branch has a fork, it must be divided into 2 forks (that is, there are no only 1 sons of nodes) this tree has 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. 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.

Enter a description input Description

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.

outputs description output Description

The maximum number of remaining apples.

sample input to sample

5 2

1 3 1

1 4 10

2 3 20

3 5 20

Sample output Sample outputs

21st

data size & Hint

For 20% data n<=20;

For 100% data 1<n<=100,1<=q<= N.

Problems encountered: The given node does not have a parent-child relationship, need to build two-way side to go DFS when the judge, this part by its attention, to determine whether to return to the parent, in the Update Father node

The code is as follows

#include <stdio.h>#include<algorithm>using namespacestd;intn,q,cnt,f[ About][ About],fa[ About],first[ About];structedge{intTo,next,val;} edge[ About];voidAddint  from,intTo,intval) {edge[++cnt].to=to ; Edge[cnt].val=Val; Edge[cnt].next=first[ from]; first[ from]=CNT;}voidDfsint  from){     for(inti=first[ from];i;i=Edge[i].next) {        intto=edge[i].to; if(To = = fa[ from])Continue; Fa[to]= from;        DFS (to);  for(intj=q;j>=1;--j)//using backward Pushing, j is the total number of sides             for(intk=0; k<j;++k)//K is the number of child nodes, cannot exceed the total number of branchesf[ from][j]=max (f[ from][j],f[to][k]+f[ from][j-k-1]+edge[i].val);//F[to][k] Sub-node having K-branch Val, F[from][j-k-1] parent node remaining branch of Val,edge[i].val current branch of Val    }}intMain () {scanf ("%d%d",&n,&q);  for(intI=1; i<n;++i) {int  from, To,val; scanf ("%d%d%d",& from,&to,&val); Add ( from, To,val); Add (To, from, Val); } DFS (1); printf ("%d", f[1][q]); return 0;}

Two-fork apple tree (Codevs 5565) tree DP

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.