HDU 5293 tree chain problem lct + tree dp, hdu5293
Question Link
Code Link
Link
Tree chain problem
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 35 Accepted Submission (s): 8
Problem DescriptionCoco has a tree, whose vertices are conveniently labeled by 1, 2 ,..., N.
There are m chain on the tree, Each chain has a certain weight. Coco wocould like to pick out some chains any two of which do not share common vertices.
Find out the maximum sum of the weight Coco can pick
InputThe input consists of several test cases. The first line of input gives the number of test cases T (T <= 10 ).
For each tests:
First line two positive integers n, m. (1 <= n, m <= 100000)
The following (n-1) lines contain 2 integers ai bi denoting an edge between vertices ai and bi (1 ≤ ai, bi ≤ n ),
Next m lines each three numbers u, v and val (1 ≤ u, v ≤ n, 0 <val <1000), represent the two end points and the weight of a tree chain.
OutputFor each tests:
A single integer, the maximum number of paths.
Sample Input1 7 3 1 1 1 3 2 4 2 5 3 6 3 7 2 4 4 5 6 7 3
Sample Output6
Hint
Stack expansion program: # pragma comment (linker, "/STACK: 1024000000,1024000000 ″)
Source2015 Multi-University Training Contest 1
----------------------
Question:
The value of m paths and paths in trees and trees with n points
Select some paths so that the selected path does not have a public point, so as to find the greatest value and.
Ideas:
Obviously there are two dp equations. Each vertex has two States, either or not.
Consider u and u's sons v1, v2, v3 ···
If dp [u] [0] is not selected, it is clear that dp [u] [0] = sigma (dp [v]);
If there is a path {u, v1, z1, z2}
When this path is selected: dp [u] [1] = (dp [u] [0]-dp [v1] [1]) -(dp [v1] [0]-dp [z1] [1]) + (dp [z1] [0]-dp [z2] [1]) + dp [z2] [0];
Get the formula: dp [u] [1] = \
Dp [u] [0]-(dp [v1] [0]-dp [v1] [1]) + (dp [z1] [0]-dp [z1] [1]) + (dp [z2] [0]-dp [z2] [1]);
Therefore, we need to maintain the weights of dp [v] [0] and dp [v] [1] on the chain, and maintain these two values using lct.
The rest is to run in the tree dp while calculating the dp value.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.