HDU 5293 TREE CHAIN PROBLEM LCT+樹形DP,hdu5293

來源:互聯網
上載者:User

HDU 5293 TREE CHAIN PROBLEM LCT+樹形DP,hdu5293

題解連結

代碼連結

連結

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 DescriptionCoco 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

 

InputThe 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.

 

OutputFor each tests:
A single integer, the maximum number of paths.

 

Sample Input1 7 3 1 2 1 3 2 4 2 5 3 6 3 7 2 3 4 4 5 3 6 7 3

 

Sample Output6

Hint

Stack expansion program: #pragma comment(linker, “/STACK:1024000000,1024000000″)

 

 

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


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.