poj 1947 Rebuilding Roads(树形dp)
来源:互联网 发布:java split函数 编辑:程序博客网 时间:2024/05/29 21:16
Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8816 Accepted: 3974
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.
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.
* 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 61 21 31 41 52 62 72 84 94 104 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;
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
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;}
0 0
- poj 1947 Rebuilding Roads(树形dp)
- POJ 1947 Rebuilding Roads(树形DP)
- POJ 1947 Rebuilding Roads(树形DP)
- POJ 1947 Rebuilding Roads (树形dp)
- 树形dp(poj 1947 Rebuilding Roads )
- POJ--1947[Rebuilding Roads] 树形DP
- POJ 1947 Rebuilding Roads 树形DP
- POJ 1947 Rebuilding Roads 树形dp
- poj 1947 Rebuilding Roads 树形dp
- poj 1947 Rebuilding Roads(树形DP)
- POJ--1947--Rebuilding Roads--树形DP
- POJ 1947 Rebuilding Roads 树形DP
- poj 1947 Rebuilding Roads 树形dp
- poj 1947 Rebuilding Roads (树形背包dp)
- poj 1947 Rebuilding Roads(树形DP)
- poj 1947 Rebuilding Roads ---树形DP
- poj 1947 Rebuilding Roads(树形DP)
- poj 1947 Rebuilding Roads (树形dp)
- DateTime之时间比较(Compare 方法)
- HDU 4839 The Game of Coins _(:зゝ∠)_
- Scheme 的 xml文件读写
- "",IDC_FRAME,"Static",SS_BLACKFRAME,226,8,20,20 CONTROL IDB_BACK,IDC_BACK,"Static",SS_
- HDOJ 1874 畅通工程续——结构体模拟邻接链表的SPFA算法
- poj 1947 Rebuilding Roads(树形dp)
- [Matlab][数据挖掘算法][线性回归]
- Linux下安装MySQL数据库以及用C语言编程存取数据库
- leetcode - Remove Duplicates from Sorted List
- 访问权限控制
- dhu 1180 诡异的楼梯
- Android应用中使用百度地图API并添加标注(一)
- 腾讯2014实习生笔试题(重庆站)
- MPI之自己实现Alltoall()函数