**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 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 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** **#1** **:**

5 2

1 3 1

1 4 10

2 3 20

3 5 20

**Output Sample** **#1** **:**

21st

**Algorithm:**

**Tree-shaped DP**

**Analysis:**

**The problem is actually to calculate the maximum weight for a given number of branches on a binary tree.**

**This is a basic tree-shaped DP template problem, which is a dynamic rule on a binary tree. We find that there is a relationship between the root node and the two subtree for any subtrees tree in the tree, and we explore that the tree is obtained by recursive nature, so we first have to build the data we get on an array.**

**After the tree has been built, we can explore its state transition equation, we know the n-1 edge, for convenience, we set it to two-way side. I set len[x][y] and len[y][x] to indicate that the X-to-y edge weights are len[x][y] (or len[y][x]).**

**Next, set Num[v] for the length of the edge with V as the sub-node, set F[V][K] to retain the maximum weight of the K-bar in the subtree of the root node of V, set Tr[v][ans] to the left and right son of the root node of V, when ans said 1 o'clock, he is the right son, and vice versa.**

**It would be much easier to put a tree in a linear DP. I asked for the current F[v][k], I first asked his left and right son to share what the maximum value of the K-bar, respectively, and then add. **

**The state transition equations can be listed as:F[v][k]=max (F[v][k],f[tr[v][1]][i]+f[tr[v][2]][k-i-1]+num[v]);**

**This thing believes not to be ugly understand, next we need to optimize the program. Since the tree can be built by hand, can we also solve it with recursion? Well, that's OK.**

**This is where we can put the normal DP on DFS to do and synthesize the memory search. Because there are some branches that I have calculated before, then I can use them directly.**

**To say a few points, the final output of the answer to output q+1, because in the calculation process for the sake of convenience, the side of the problem is transformed into a parent node or sub-node problem, so we need to +1 in the number of nodes.**

**Remember to initialize a negative number in the Len array, and when you are done with one of the edges, make sure that the edge and the other equivalent edge are deleted. **

**On the code:**

1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 intn,q,tr[ the][3],num[ the],len[ the][ the],f[ the][ the];//data size is small, int type is enough6 7InlineintRead ()//read-in optimization8 {9 intx=0, f=1;Ten CharC=GetChar (); One while(c< -|| C> $) Af=c=='-'?-1:1, c=GetChar (); - while(c>= -&&c<= $) -X= (x<<1) + (x<<3) + (c^ -), c=GetChar (); the returnx*F; - } - - voidBuildtree (intV//Achievements + { - inti,ans=0; + for(i=1; i<=n;i++) A if(len[v][i]>=0)//there's a fork! at { -ans++;//record and judge the left and right sub-trees -tr[v][ans]=i; -Num[i]=len[v][i];//record length -len[v][i]=len[i][v]=-1;//Delete two edges -Buildtree (i);//recursive its child node in if(ans==2)//when we get there, we return. - return; to } + } - the voidDfsintVintK//Memory Search * { $ if(k==0)//BorderPanax Notoginsengf[v][k]=0; - Else the if(tr[v][1]==0&&tr[v][2]==0)//to the leaf node, whose value is equal to the original value +f[v][k]+=Num[v]; A Else the { +f[v][k]=0;//First Clear 0 - inti,j; $ for(i=0; i<k;i++)//enumeration of 0 to K-1 $ { - if(!f[tr[v][1]][i])//memory of the left son -DFS (tr[v][1],i); the if(!f[tr[v][2]][k-i-1])//memory of the right son -DFS (tr[v][2],k-i-1);WuyiF[v][k]=max (f[v][k],f[tr[v][1]][i]+f[tr[v][2]][k-i-1]+NUM[V]);//State Transitions the } - } Wu } - About intMain () $ { - inti,j; -n=read (); -q=read (); A for(i=1; i<=n;i++)//Initialize + for(j=1; j<=n;j++) thelen[i][j]=-1; - for(i=1; i<=n-1; i++) $ { the intX=read (), y=read (); thelen[x][y]=len[y][x]=read (); the } theBuildtree (1);//Achievements -Dfs1, q+1);//solving inprintf"%d", f[1][q+1]);//Note, Remember, it's q+1 . the return 0; the}

**This is a binary tree base DP, I feel very good understanding, the main model is recursive achievements + memory search DP, simply recorded as 2 Dfs.**

**Well, that's it.**

"Luo gu P2015" two-prong apple tree