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 Description
Coco 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 would 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
Input
The 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.
Output
For each tests:
A single integer, the maximum number of paths.
Sample Input
1 7 3 1 2 1 3 2 4 2 5 3 6 3 7 2 3 4 4 5 3 6 7 3
Sample Output
6
Hint
Stack expansion program: #pragma comment(linker, “/STACK:1024000000,1024000000″)
Source
2015 Multi-University Training Contest 1
——————————————————————
题意:
给定n个点的树 和树上的m条路径以及路径的价值
选择一些路径使得选的路径没有公共点,求最大的价值和。
思路:
显然就是2个dp方程,每个点有两个状态,选或不选。
考虑u和u的儿子们v1,v2,v3···
设dp[u][0]是不选,显然dp[u][0] = sigma(dp[v]);
若有一条路径{u,v1,z1,z2}
则选择这条路径时:dp[u][1] =(dp[u][0]-dp[v1][1])-(dp[v1][0]-dp[z1][1])+(dp[z1][0]-dp[z2][1])+dp[z2][0];
将公式移项可得:dp[u][1] = \
dp[u][0]-(dp[v1][0]-dp[v1][1])+(dp[z1][0]-dp[z1][1])+(dp[z2][0]-dp[z2][1]);
所以我们需要维护链上的dp[v][0]和dp[v][1]的权值和,用lct维护一下这两个值。
剩下就是在树形dp一边跑,一边计算dp的值了。
版权声明:本文为博主原创文章,未经博主允许不得转载。