Two-fork Apple Tree 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 and Output Sample input example # #:
5 21 3 11 4 102 3 203 5 20
Sample # # of output:
21st
To make a tree cut off K nodes the whole tree weights and maximum. The subject is quite disgusting, and it is troublesome to deal with it. Because although the title is guaranteed to be two forks, he did not give us the order of the two points when he entered it. That's him.
Enter U,v,w, you do not know whether U is the father of V or V is the father of U. There are many people on the Internet to write this puzzle to facilitate the mess, judge what if that point left son is empty become left son otherwise become right son of what ghost, General
It's all over the water. CV water But luogu or reverse deflection. The correct posture should be stored with a side table. Like my approach is to take the side table store and get a find_father function to find each node's father. About this function
I'll just pull it out for a minute. I did a lot more than I could do without working hard: I ran again. Enumerate the left and right sons of each node. After the trouble in front of the work, we can then consider DP.
Since the weight of the title is the right side, we can do a dirty operation: The weight of each edge is moved down to the edge of the son node, and then to the root node of the weight of 0. Because we gave the root node 11 weights of 0, the
The number of nodes we want to keep is changed from Q to Q+1! Then we can continue the Sao operation, using DP (I,J) to represent the retention of J nodes, so we start with the DP root node: DP (1,Q+1). So how do you write this DP function?
It?
We set up a subtree to keep j nodes, and the root node must be saved, because if you cut the root node from the beginning, you're out of the business. Therefore, the root node of the two sons of the subtree root to save a total of j-1 nodes.
With the idea, we can set the left subtree to save K nodes, and then we enumerate this k, so the right subtree can be pushed out to save j-k-1 nodes. Then we'll find the state transfer equation and the maximum value.
In the construction of ...
Two-prong apple tree |codevs5565|luogup2015| tree-shaped dp| Elena