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


發佈了870 篇原創文章 · 獲贊 267 · 訪問量 201萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章