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] ...
随机推荐
- close与shutdown函数
linux网络编程之socket(十):shutdown 与 close 函数的区别 http://blog.csdn.net/yijiu0711/article/details/17349169 ...
- 【leetcode】Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- XML文件的读取----cElementTree
XML文件如下: <?xml version="1.0" encoding="UTF-8"?> <tokenxml> <token ...
- javascript 数组去重
2015年5月15日 20:17:05 星期五 原理: .......(说不清楚, 自己看代码吧, 很简单.....) //去重 var hash_already_input = {}; for (v ...
- Linux下常用的硬件信息查看命令
1.查看CPU型号,这里为了方便查看结合管道符用grep进行了匹配,当然只需要前面的命令也可以,命令如下: cat /proc/cpuinfo | grep "model name" ...
- Selenium 模拟人输入
public static void type(WebElement e,String str) throws InterruptedException { String[] singleCharac ...
- SAP ALV显示并打印(非OO方式)
*&---------------------------------------------------------------------* *& Report Z_SD_CPF ...
- Effective C++ -----条款11: 在operator=中处理“自我赋值”
确保当对象自我赋值时operator=有良好行为.其中技术包括比较“来源 对象”和“目标对象”的地址.精心周到的语句顺序.以及copy-and-swap. 确定任何函数如果操作一个以上的对象,而其中多 ...
- tableview详细介绍
tableview详细介绍: https://www.baidu.com/link?url=MU5a5om66vYEKAcnvmXCeCwMGetezW5o2X11OUnwN7-fb_jWPx6xyv ...
- [Android Pro] CPU占用计算方法
1: AVTest CPU计算方法读取每个进程的 stat 文件 (/proc/<PID>/stat)计算采样间隔10min下utime的差值minusUtime,stime的差值min ...