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/Output sample
Input Sample:
5 21 3 11 4 102 3 203 5 20
Sample output:
21st
For a tree DP, it is a dynamic plan on top of the tree. The key point is the hierarchical nature of the tree, and the hierarchy is realized by recursive achievements. Note that this problem has a root tree, the root node given is 1, and must be preserved!
It's written in the comments.
The code is as follows:
1#include <bits/stdc++.h>2 3 using namespacestd;4 #defineINF 0x3f3f3f3f5 #defineM 50006 intnext[m],pre[m],last[m],apple[m],dp[m][m],n,m,tot=0;7 /*8 Dp[i][j] Indicates that node I retains the remaining Apple Max for J branches9 Apple[i] Indicates the number of apples on the edge of article ITen Next,pre,last is an array for building edges. One tot to count the number of edges A */ - voidCNCT (intUintVintW) - { thetot++; -next[tot]=v; -pre[tot]=Last[u]; -last[u]=tot; +apple[tot]=W; - } + intDFS (intUintfather) A { at intans=0;//ans Indicates the number of child nodes of the U node - for(inti=last[u];i!=0; i=Pre[i]) - { - intV=next[i],value=Apple[i]; - if(v = = father)Continue;//If the next neighbor is the parent node, it proves to the bottom, and begins to recursively parent the sibling node -Ans+=dfs (V,u) +1;//recursion to the top-level root node 1 in for(intJ=min (ans,m); j>=1;--j)//because there are limits on the number of branches, take a min - { to for(intK=min (J,ans); k>=1;--k) +Dp[u][j]=max (dp[u][j],dp[u][j-k]+dp[v][k-1]+value); - /* the for the Sub-node J under the U node, the optimal number of branches for J retention DP * here is a good explanation, because we are the tree that we built according to the recursive. $ when comparing, the dp[u][j] is the result of the optimal solution of a sub-node that is selected in the front except IPanax Notoginseng so it's not possible to repeat or miss a node when DP - */ the } + } A returnans; the } + intMain () - { $ //freopen ("De.txt", "R", stdin); $Memset (Last,0,sizeofLast ); -Memset (Next,0,sizeofnext); -memset (PRE,0,sizeofpre); theMemset (DP,0,sizeofDP); -scanf"%d%d",&n,&m);Wuyi for(intI=1; i<n;++i) the { - intX,y,value; Wuscanf"%d%d%d",&x,&y,&value); - cnct (x,y,value); About cnct (y,x,value); $ } -Dfs1,0); -printf"%d\n", dp[1][m]); - return 0; A } +
Rokua P2015 Two-prong apple tree (tree-like DP)