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] ...
随机推荐
- linux学习之-vsftp
一.简介 vsftp是一个基于GPL发布的类unix系统上使用的ftp服务器软件,它的全称是very secure FTP ,软件的编写初衷是为了代码的安全,另外高速与高稳定性也是vsftp的两个重要 ...
- 阿里2014校招笔试题(南大)——利用thread和sleep生成字符串的伪随机序列
引言:题目具体描述记不大清了,大概是:Linux平台,利用线程调度的随机性和sleep的不准确性,生成一个各位均不相同的字符数组的伪随机序列.不得使用任何库函数.(这句记得清楚,当时在想线程库算不算, ...
- 几种Linux 查询外网出口IP的方法
Curl 纯文本格式输出: curl icanhazip.com curl ifconfig.me curl curlmyip.com curl ip.appspot.com curl ipinfo. ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- android入门到熟练(五)---广播
1.广播类型:标准广播和有序广播.标准广播是异步广播在广播发出之后所有接收器几乎会同一时刻接收到,没有先后顺序,效率高,但无法被截断.有序广播则是同步广播,同一时刻只能一个接收器接收这条消息,等执行完 ...
- 【linux】学习2
鸟哥那本书的第6章 文件权限: ^ ^ ^ ^ ^ ^ ^ 1 ...
- iOS-WKWebView携带cookie发送http请求,cookie失效
发送请求代码: NSString *testUrl = @"http://10.22.122.7:8081/test2_action/view_index"; NSURL *url ...
- Qt 安装一个Service
QString command = "sc create YourServiceName binPath= \""+application_path+"\&qu ...
- 项目之solr全文搜索工具的安装
1. Solr简介 Solr是一个基于Lucene的Java搜索引擎服务器.Solr 提供了层面搜索.命中醒目显示并且支持多种输出格式(包括 XML/XSLT 和 JSON 格式).它易于安装和配置, ...
- android中点击空白处隐藏软键盘
InputMethodManager manager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERV ...