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

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的值了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 07:49:35

HDU 5293 TREE CHAIN PROBLEM LCT+树形DP的相关文章

hdu 5293 Tree chain problem(树链剖分+树形dp)

题目链接:hdu 5293 Tree chain problem 维护dp[u], sum[u],dp[u]表示以u为根节点的子树的最优值.sum[u]表示以u节点的所有子节点的dp[v]之和.对于边a,b,w,在LCA(a,b)节点的时候进行考虑.dp[u] = min{dp[u], Sum(a,b) - Dp(a,b) + sum[u] | (ab链上的点,不包括u } #pragma comment(linker, "/STACK:1024000000,1024000000")

HDU 5293 Tree chain problem

Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 565    Accepted Submission(s): 137 Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,-,n. Th

(中等) HDU 5293 Tree chain problem,树链剖分+树形DP。

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

HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形dp, 对于每条链u,v,w,我们只在lca(u,v)的顶点上处理它 让dp[i]表示以i为根的指数的最大值,sum[i]表示dp[vi]的和(vi为i的儿子们) 则i点有两种决策,一种是不选以i为lca的链,则dp[i]=sum[i]. 另一种是选一条以i为lca的链,那么有转移方程:dp[i]=

HDOJ 5293 Tree chain problem LCA+树链剖分+树形DP

[题意] 给定一颗树上的几条链和每条链的权值,求能取出的不含有公共节点的链的最大权值.... [解] 预处理每条链的lca 树形DP, d[i]表示取到这个节点时可以得到的最大值 , sum[i]=sigma( d[k] | k 是i的子节点) 如果不取i  d[i]=sum[i] 如果取i , e是lca为i的链则 d[i]=max(d[i],e的权值+sigma(sum[k])-sigma(d[k]))  k为树链上的点 可以用树链剖分+树装数组在nlogn的时间复杂度内求链上的值 Tree

HDU 5293(Tree chain problem-树链剖分)

Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 940    Accepted Submission(s): 248 Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,-,n. Th

hdu5293(2015多校1)--Tree chain problem(树状dp)

Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 262    Accepted Submission(s): 59 Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,-,n. The

hdu 1561The more, The Better(树形dp&amp;01背包)

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4949    Accepted Submission(s): 2918 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

hdu 1011 Starship Troopers (依赖背包 树形dp)

题目: 链接:点击打开链接 题意: n个房间组成一棵树,你有m个战队,从1号房间开始依次clear每个房间,在每个房间需要花费的战队个数是bugs/20,得到的价值是the possibility of capturing a brain,求最大的价值. 算法: 树形dp,有依赖的背包问题.(依次clear每个房间) 思路: 状态转移dp[i][j]表示根结点为i时(房间i)花费j个战队能够得到的最大价值(捕捉到一个brain最大的可能值).递归求出每个根结点处的最大值,最后dp[1][m]就是