HDU 5293 TREE CHAIN PROBLEM LCT+树形DP

题解链接

代码链接

链接

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

——————————————————————
题意:
给定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的值了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值