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
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
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.
A single integer, the maximum number of paths.
Stack expansion program: #pragma comment(linker, “/STACK:1024000000,1024000000″)
——————————————————————
題意:
給定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的值了。