Two-prong apple tree (tree dp+ backpack)

Source: Internet
Author: User

 two fork Apple tree
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 is 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.
Program Name: Apple
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 Sample:

5 2
1 3 1
1 4 10
2 3 20
3 5 20
Input Sample:

21st

Problem-Solving ideas: Tree-type dp+ backpack solution.

F (I, j) represents the subtree I, preserving the maximum weights of J nodes (note is the node). The weight of each edge is considered to be the weight of the son node in the connected two nodes.
then, you can do a group of all I sub-tree backpack, that is, each subtree can choose to,... j-1 bar edge assigned to it.
state transitions to:
F (i, j) = max{max{f (i, j-k) + f (V, k) | 1<=k<j} | V is the son of I}
ans = f (1, q+1)

The code is as follows:            

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < vector>using namespace std; #define MAX 105#define INF 999999999#define MP make_pairtypedef pair<int,int> PII; Vector<pii> adj[max];int tot[max],f[max][max];int MAX (int a,int b) {return a>b?a:b;} int DFS (int u,int ff) {tot[u]=1;int i,j,k;for (i=0;i<adj[u].size (); i++) {int v=adj[u][i].first;if (V==FF) continue; Tot[u]+=dfs (v,u);} For (I=0;i<adj[u].size (); i++) {int v=adj[u][i].first;int w=adj[u][i].second;if (v==ff) continue;for (j=tot[u];j >1;j--) {for (k=1; (k<j) && (K<=tot[v]); k++) F[u][j]=max (F[U][J],F[U][J-K]+F[V][K]+W);}} return tot[u];} int main () {int I;int n,p,u,v,w;while (~scanf ("%d%d", &n,&p)) {for (i=0;i<max;i++) adj[i].clear (); for (i=1;i <n;i++) {scanf ("%d%d%d", &u,&v,&w), Adj[u].push_back (MP (V,W)); Adj[v].push_back (MP (U,W));} memset (F,0,sizeof (f));D FS (1,-1);p rintf ("%d\n", F[1][p+1]);} return 0;}

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.