POJ1947 Rebuilding Roads(樹狀DP)

                                                                          Rebuilding Roads
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3698   Accepted: 1517

Description

The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get from any given barn to any other barn. Thus, the farm transportation system can be represented as a tree.

Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.

Input

* Line 1: Two integers, N and P

* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads.

Output

A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.

Sample Input

11 6
1 2
1 3
1 4
1 5
2 6
2 7
2 8
4 9
4 10
4 11

Sample Output

2
這題是我的第二個樹狀DP,還是看着一個大牛的代碼寫的。還是要更加努力呀,努力    加油。。。。。。
每個節點都有兩種不同的狀態:
1.該點被包含在剩下的樹,其子樹則不被包含
2.該點不被包含在剩下的樹,其子樹判斷和該點一樣。
dp[i,j]表示節點i爲根節點得到剩下j個節點的樹所刪邊的最小數目
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章