Rokua P2015 two fork apple tree

Source: Internet
Author: User

P2015 Two-prong apple treeTitle 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 this branch is too much to be pruned. 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 formatInput 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/Output sampleInput Sample # #:
5 21 3 11 4 102 3 203 5 20
Sample # # of output:
21st
Idea: Similar to the previous topic selection course, the same is the tree-shaped DP, (memory search).
The wrong reason: the two-way chain table storage, the array of wood opened twice times, (╯‵-′) ╯︵┻━┻.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intN,q,tot;intf[ the][ the];intto[ About],net[ About],head[ About],cap[ About];intdad[ the],lchild[ the],rchild[ the],w[ the];voidAddintUintVintW) {to[++tot]=v;net[tot]=head[u];cap[tot]=w;head[u]=tot; to[++tot]=u;net[tot]=head[v];cap[tot]=w;head[v]=tot;}voidDfsintNow ) {     for(intI=head[now];i;i=Net[i]) {        if(dad[now]!=To[i]) {Dad[to[i]]=Now ; W[to[i]]=Cap[i];        DFS (To[i]); }    }}intDfsintIintj) {    if(i<1|| j<1|| i>n| | j>q+1)return 0; if(F[i][j])returnF[i][j];  for(intk=0; k<j;k++) F[i][j]=max (F[i][j],dfs (lchild[i],k) +dfs (rchild[i],j-k-1)+W[i]); F[I][J]=Max (F[i][j],dfs (rchild[i],j)); returnf[i][j];}intMain () {scanf ("%d%d",&n,&q);  for(intI=1; i<n;i++){        intx, Y, Z scanf ("%d%d%d",&x,&y,&z);    Add (x, y, z); } Add (1,0,0); DFS (0);  for(intI=1; i<=n;i++){        intHasDad[i]; if(!lchild[fa]) lchild[fa]=i; Else{FA=LCHILD[FA];  while(Rchild[fa]) fa=RCHILD[FA]; RCHILD[FA]=i; }} cout<<dfs (lchild[0],q+1);}

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