Brush Title Summary--Two fork apple tree (ssoj tree-shaped dp+ memory search)

Source: Internet
Author: User

Title: Topic background

ural:http://acm.timus.ru/problem.aspx?space=1&num=1018

Title 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, the 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. 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.

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 are the number of nodes it connects to, and 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.

Sample Data 1

Enter [Copy]

5 2
1 3 1
1 4 10
2 3 20
3 5

Output

+
Exercises

DP[I][J]: Indicates the subtree of point I (including myself) the maximum number of apples that can be left by a J-point

Pay attention to this problem, I turn the reserved m edge into a reserved m+1 point to ask for

Also note that the memory search When you are doing a tree DP Dfs

Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<ctime>#include<cctype>#include<cstring>#include<string>#include<algorithm>using namespacestd;Const intn= the;intfirst[n],next[n*2],go[n*2],val[n*2],tot;intn,m;intDp[n][n];inlinevoidComb (intAintBintc) {next[++tot]=first[a],first[a]=tot,go[tot]=b,val[tot]=C; next[++tot]=first[b],first[b]=tot,go[tot]=a,val[tot]=C;} InlinevoidDfsintUintFaintVal,intk) {  if(u==0|| k==0) {Dp[u][k]=0; return; }  if(dp[u][k]!=-1)    return; DP[U][K]=0;  for(intI=0; i<k;i++)  {     intL=0, r=0, VL,VR;  for(intE=first[u];e;e=Next[e]) {      intv=Go[e]; if(V==FA)Continue; if(l==0) L=v,vl=Val[e]; Else{R=v,vr=Val[e];  Break;    }} dfs (L,u,vl,i); DFS (R,u,vr,k-i-1); DP[U][K]=max (dp[l][i]+dp[r][k-i-1]+Val,dp[u][k]); }  return;}intMain () {Freopen ("a.in","R", stdin); scanf ("%d%d",&n,&m); M++; Memset (DP,-1,sizeof(DP)); intA,b,c;  for(intI=1; i<n;i++) {scanf ("%d%d%d",&a,&b,&c);  Comb (A,B,C); } DFS (1,0,0, M); cout<<dp[1][m]<<Endl; return 0;}

Brush Title Summary--Two fork apple tree (ssoj tree-shaped dp+ memory search)

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.