[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] ...
随机推荐
- 关于RPG游戏结构撰写的相关探索下篇
如今市面上已经有好几百种免费RPG系统,我们都能够按照自己的需求对此进行扩展与修改.通过选择现有的系统(特别是较有名的),你能够从一个稳定且经过测试的基础开始创 造. 但是之后你需要基于设置和规则对此 ...
- 使用CountDownTimer实现倒计时功能
// 倒计时60s new CountDownTimer(60000, 1000) { @Override public void onTick(long millisUntilFinished) { ...
- Python模块学习——tempfile
主要有以下几个函数: tempfile.TemporaryFile 如何你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么用TemporaryFile函数创建临时文件是最好的选择.其 ...
- html+css+jQuery+JavaScript实现tab自动切换功能
tab1.html内容 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- 如何在 Django 中保证并发的数据一致性
1. 关于锁 1.1 乐观锁 乐观锁的出发点是,同一条数据很少会因为并发修改而产生冲突,适用于读多写少的场景,用以提高吞吐量. 实现方式,读取一个字段,执行处理逻辑,当需要更新数据时,再次检查该字段是 ...
- C盘空间不够,清除VS下的 Font Cache
C盘空间老是不够用.清除Font Cache 1.在 C:\Users\Jimmy\AppData\Local\Microsoft\Visual Studio 下的 Font Cache 目录可以干 ...
- nginx报502 bad GateWay错误的解决方法
nginx+php-fpm+mysql的网站,访问nginx的某个页面,报502 GateWay的错误,一般见到此错误,可以判断是php-fpm的问题,而不是nginx的问题.通过监控nginx的错误 ...
- mariadb是替代MySQL的好方式
像Oracle这样成熟稳定的数据库,足以支撑海量数据的存储与查询了?为什么还需要数据切片呢?的确,Oracle的DB确实很成熟很稳定,但是高昂的使用费用和高端的硬件支撑不是每一个公司能支付的起的.试想 ...
- PowerDesigner导入java类生成类图
1;打开PowerDesigner 2;file—>Reverse Engineer—>Object Language... 3;弹出一个对话框,在General模块下Model Name ...
- IIS7 https 发生413错误 未显示页面,因为请求实体过大
参考文档: http://msdn.microsoft.com/zh-cn/library/cc737382(v=ws.10).aspx http://www.java123.net/v/12 ...