POJ 1947 Rebuilding Roads 树形DP
Description
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
* 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
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
题意:
给你一个n点的树和一个p
问你通过删除一些边得到一个至少含有一个子树节点数为p的最少删除数
题解:
设定dp[u][x]表示以u为根节点剩余x个点的最少删除边数
那么这就是背包问题了
dp[u][i] = min(dp[v][k]+dp[u][i-k]-1,dp[u][i]);
u表示根节点,v表示儿子之一
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include<vector>
#include <algorithm>
using namespace std;
const int N = 2e2+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int siz[N],n,p,dp[N][N];
vector<int > G[N];
void dfs(int u,int fa) {
siz[u] = ;
int totson = G[u].size();
for(int i=;i<totson;i++) {
int to = G[u][i];
if(to == fa) continue;
dfs(to,u);
siz[u] += siz[to];
}
dp[u][] = totson - ;if(u == ) dp[u][]++;
for(int j=;j<totson;j++) {
int v = G[u][j];
if(v == fa) continue;
for(int i=siz[u];i>=;i--) {
for(int k=;k<i && k<=siz[v];k++) {
dp[u][i] = min(dp[v][k]+dp[u][i-k]-,dp[u][i]);
}
}
}
}
int main()
{
scanf("%d%d",&n,&p);
for(int i=;i<n;i++) {
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
for(int i=;i<=n;i++) for(int j=;j<=p;j++) dp[i][j]=inf;
dfs(,-);
int ans = dp[][p];
for(int i=;i<=n;i++) {
ans = min(ans, dp[i][p]+);
}
cout<<ans<<endl;
}
POJ 1947 Rebuilding Roads 树形DP的更多相关文章
- POJ 1947 Rebuilding Roads 树形dp 难度:2
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9105 Accepted: 4122 ...
- DP Intro - poj 1947 Rebuilding Roads(树形DP)
版权声明:本文为博主原创文章,未经博主允许不得转载. Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissi ...
- [poj 1947] Rebuilding Roads 树形DP
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10653 Accepted: 4884 Des ...
- POJ 1947 Rebuilding Road(树形DP)
Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...
- POJ 1947 Rebuilding Roads (树dp + 背包思想)
题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...
- 树形dp(poj 1947 Rebuilding Roads )
题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
- POJ1947 - Rebuilding Roads(树形DP)
题目大意 给定一棵n个结点的树,问最少需要删除多少条边使得某棵子树的结点个数为p 题解 很经典的树形DP~~~直接上方程吧 dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v] ...
- POJ 1947 Rebuilding Roads(树形DP)
题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...
随机推荐
- ACL
http://man.chinaunix.net/linux/debian/debian_learning/ch01s04.html http://blog.csdn.net/xiangliangyu ...
- zju3547
题意:给出n(1<=n<=10^8),求小于n的,求所有与n互质的数字的四次幂的加和是多少. 分析:容斥原理 首先要知道四次幂求和公式,1^4+2^4+...+n^4=n*(n+1)*(2 ...
- ios 在中国地区,24小时时间格式 系统设定下 获得12小时制时间的方法
如题,在中国地区,24小时时间格式 系统设定下,如果单单使用 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 之后,无论用hh ...
- oracle数据库迁移---windows环境下
以前在学校只是听过oracle,但是从来没有接触过.最近公司突然给了我一个任务,让我将某个大型商场的网站迁移到与服务器上面. 当时也觉得,迁移个网站也就是个很简单的事情,将文件复制,拷贝下就可以了撒. ...
- ACM/ICPC 之 最短路径-dijkstra范例(ZOJ2750-POJ1135(ZOJ1298))
最短路经典算法-dijkstra范例(两道),第一道是裸的dijkstra,第二道需要枚举所有边已找到可能的情况. ZOJ2750-Idiomatic Phrases Game 题意:见Code 题解 ...
- poj 2389.Bull Math 解题报告
题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次 ...
- 由浅入深剖析.htaccess
转自:http://blog.csdn.net/21aspnet/article/details/6908025 [-] htaccess文件使用前提 htaccess基本语法介绍 现学现用学习正则表 ...
- objective-c数组
1 #pragma mark -----------数组的初始化方式-------------- 2 // insert code here... 3 // NSLo ...
- android中点击空白处隐藏软键盘
InputMethodManager manager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERV ...
- hdu1212(大数取模)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 题意:给出两个数a, b,求a%b: 思路:(c+d)%e=c%e+d%e,(c*d)%e=(c ...