poj 1947 Rebuilding Roads(树形dp)
2014-05-31 21:15
435 查看
Rebuilding Roads
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
Sample Output
Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.]
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/11/b6ee1ae8e4f0fb4ba254039d7f550bcd)
初始化:
road[i]表示第i个节点有多少子节点,
if i 为根节点:dp[i][1] = road[i];
else dp[i][1] = road[i]+1;
其余dp[i][j]可以根据儿子节点的值,用背包算出!
WA了好多次,是背包写反了!
for i = 0 to P //遍历儿子节点中的dp[s][i]
for j = P to 0 //01背包从尾部开始放
dp[f][j+i] = min(dp[f][j]+dp[s][i]-2 , dp[f][j+i])
错!!
应该是:
for j = P to 0 //01背包从尾部开始放
for i = 0 to P //遍历儿子节点中的dp[s][i]
dp[f][j+i] = min(dp[f][j]+dp[s][i]-2 , dp[f][j+i])
否则,同一个儿子节点的i值会互相影响!
跑一下下面样例就会发现错误:
16 5
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
2
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 8816 | Accepted: 3974 |
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
Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.]
初始化:
road[i]表示第i个节点有多少子节点,
if i 为根节点:dp[i][1] = road[i];
else dp[i][1] = road[i]+1;
其余dp[i][j]可以根据儿子节点的值,用背包算出!
WA了好多次,是背包写反了!
for i = 0 to P //遍历儿子节点中的dp[s][i]
for j = P to 0 //01背包从尾部开始放
dp[f][j+i] = min(dp[f][j]+dp[s][i]-2 , dp[f][j+i])
错!!
应该是:
for j = P to 0 //01背包从尾部开始放
for i = 0 to P //遍历儿子节点中的dp[s][i]
dp[f][j+i] = min(dp[f][j]+dp[s][i]-2 , dp[f][j+i])
否则,同一个儿子节点的i值会互相影响!
跑一下下面样例就会发现错误:
16 5
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
2
#include <iostream> #include <cstdio> #include <vector> using namespace std; const int maxn = 200; int dp[maxn][maxn]; vector<int> v[maxn]; bool son[maxn]; int N , P , ans; void initial(){ for(int i = 0; i < maxn; i++){ for(int j = 0; j < maxn; j++){ dp[i][j] = maxn; } son[i] = false; v[i].clear(); } ans = maxn; } void readcase(){ int I , J; for(int i = 0; i < N-1; i++){ scanf("%d%d" , &I , &J); v[I].push_back(J); son[J] = true; } } void DP(int f){ int road = v[f].size(); dp[f][1] = road; if(son[f]) dp[f][1]++; for(int i = 0; i < road; i++){ int s = v[f][i]; DP(s); for(int j = P; j >= 0; j--){ if(dp[f][j] != maxn){ for(int k = 0; k+j <= P; k++){ if(dp[s][k] != maxn){ dp[f][j+k] = min(dp[f][j]+dp[s][k]-2 , dp[f][j+k]); } } } } } ans = min(ans , dp[f][P]); } void computing(){ for(int i = 1; i <= N; i++){ if(son[i] == false){ DP(i); //ans = min(ans , dp[i][P]); break; } } printf("%d\n" , ans); } int main(){ while(scanf("%d%d" , &N , &P) != EOF){ initial(); readcase(); computing(); } return 0; }
相关文章推荐
- poj 1947 树形dp 背包
- poj 1947 Rebuilding Roads(树形DP)
- POJ 1947-Rebuilding Roads-(树形DP)
- Rebuilding Roads - POJ 1947 树形dp
- 树形DP______Rebuilding Roads( POJ 1947 )
- 【树形DP】【多叉转二叉】【poj 1947】Rebuilding Roads
- poj 1947 Rebuilding Roads 树形dp背包
- poj 1947 Rebuilding Roads(树形dp)
- POJ 1947 树形DP(分组背包)
- poj 1947 Rebuilding Roads ---树形DP
- poj 1947(树形dp)
- poj 1947(树形dp)
- POJ--1947--Rebuilding Roads--树形DP
- POJ 1947 树形DP
- poj 1947 树形dp(得到含P个节点联通块的最小切边数)
- 树形dp(poj 1947 Rebuilding Roads )
- Rebuilding Roads POJ - 1947 简单树形DP加背包问题
- POJ 1947 Rebuilding Roads(树形DP)
- POJ 1947 Rebuilding Roads 树形DP
- poj 1947 Rebuilding Roads 【树形DP】 【求至少删去树中 多少条边 使得树中节点数为P】