[poj 1947] Rebuilding Roads 树形DP
Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 10653 Accepted: 4884
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
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.]
Source
USACO 2002 February
题目链接:
http://poj.org/problem?id=1947
题意:
给你一棵节点为n的树。问至少砍几刀能够孤立出一棵节点为m的子树。
思路:
找到根节点(入度为0)后dfs在每一个节点统计dp[i][j]
表示i及其的子树保留j个节点的最小代价;
int tmp=dp[x][ii]+1;//不要y节点;
tmp=min(tmp,dp[x][ii-j]+dp[y][j]);
dp[x][ii]=tmp;
y为x的儿子节点。
代码:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
using namespace std;
int dp[155][155];
int ans;
int in[155];
vector<int> lin[155];
int n,aa,bb,p;
void dfs(int x)
{
for(int i=2;i<=p;i++) dp[x][i]=99999999;
if(lin[x].size()==0)
dp[x][1]=0;
for(int i=0;i<lin[x].size();i++)
{
int y=lin[x][i];
dfs(y);
for(int ii=p;ii>=1;ii--)
{
int tmp=dp[x][ii]+1;
for(int j=1;j<ii;j++)
tmp=min(tmp,dp[x][ii-j]+dp[y][j]);
dp[x][ii]=tmp;
}
}
}
int main()
{
scanf("%d%d",&n,&p);
{
int ans=99999999;
for(int i=1;i<n;i++)
{
scanf("%d%d",&aa,&bb);
lin[aa].push_back(bb);
in[bb]++;
}
for(int i=1;i<=n;i++)
if(!in[i])
{
dfs(i);
ans=dp[i][p];
break;
}
for(int i=1;i<=n;i++)
ans=min(ans,dp[i][p]+1);
printf("%d\n",ans);
}
}
[poj 1947] Rebuilding Roads 树形DP的更多相关文章
- POJ 1947 Rebuilding Roads 树形DP
Rebuilding Roads Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...
- 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 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] ...
随机推荐
- Android 编程下代码之(QQ消息列表滑动删除)
这份代码写出来有些时候了,一直没共享,现在把它共享给大家.简单列一下代码中你可以学到的知识点: 自定义控件的实现方式: 事件的拦截分发消费机制: QQ会话列表滑动删除原理: 最后附上源码链接:Q ...
- 【甘道夫】HBase基本数据操作详解【完整版,绝对精品】
引言 之前详细写了一篇HBase过滤器的文章,今天把基础的表和数据相关操作补上. 本文档参考最新(截止2014年7月16日)的官方Ref Guide.Developer API编写. 所有代码均基于“ ...
- std::bind()图解
参考:http://blog.think-async.com/2010/04/bind-illustrated.html 避免链接失效,就把文中图转过来了,这几张就清楚的说明了bind的用法和原理.
- jsdoc注释规范工具(使用 JSDoc 3 自动生成 JavaScript API 文档)
安装和使用规范见:http://moodpo.com/archives/jsdoc3-tutorial.html 实例: /** * 模块调用方法 * * * @param {string} modu ...
- RedHat中代理设置
YUM代理设置 编辑/etc/yum.conf,在最后加入 # Proxy proxy=http://username:password@proxy_ip:port/ 也可以使用proxy_usern ...
- python dict与list
本文实例讲述了python中字典(Dictionary)用法.分享给大家供大家参考.具体分析如下: 字典(Dictionary)是一种映射结构的数据类型,由无序的“键-值对”组成.字典的键必须是不可改 ...
- DIV+CSS兼容解决DIV最大宽度和最小宽度问题
在制作网页中,我们经常会碰到min/max-width,min/max-height在IE6底下是无效的,这也是web设计师最头疼的问题之一,以下的方法可以解决这些难题,并且比较简约.当然,如果你还有 ...
- [Linux实用工具]Windows下同步Linux文件(Linux安装Samba和配置)
场景需求: 安装了Ubuntu在虚拟机上,但是代码编辑或者其它更多的操作的时候,还是习惯在windows下进行.如果windows下编辑完再上传到服务器,再编译执行,就太繁琐了.一次两次还好说,这编译 ...
- Selenium学习笔记
Selenium是一个强大的自动化测试工具,它的核心思想是通过JavaScript嵌入的方式直接操纵页面的DOM来模拟手工测试工作. Selenium IDE,一个Firefox插件,可以在Firef ...
- Android(我还是个菜鸟)——UI-开源框架ImageLoader的完美例子
开源框架ImageLoader 可在文件里面找——Desktop.zip(原文件为jar格式) 使用这个框架的好处: 让图片能在异步加载更加流畅,可以显示大量图片,在拖动ListView的时候不会出现 ...