Rebuilding Roads
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11495   Accepted: 5276

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


题意:给定一棵节点数为n的树,问从这棵树最少删除几条边使得某棵子树的节点个数为p

一开始想了个倒着选了几条边,其实正着也可以,先d[i][1]=子节点数量
d[i][j]表示以i为根的子树节点数为j最少删几条边
注意size要加上自己
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=,INF=1e9;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,u,v,w,ind[N];
struct edge{
int v,w,ne;
}e[N<<];
int h[N],cnt=;
void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
}
int d[N][N],size[N];
void dfs(int u){
int child=;size[u]=;//!self
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
dfs(v);
size[u]+=size[v];
child++;
}
if(!child) {size[u]=;d[u][]=;return;}//printf("size %d %d\n",u,size[u]); d[u][]=child;
for(int j=;j<=size[u];j++) d[u][j]=INF;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
for(int j=size[u];j>=;j--){
int t=min(j-,size[v]);
for(int k=;k<=t;k++) d[u][j]=min(d[u][j],d[u][j-k]+d[v][k]-);
}
} //for(int i=1;i<=size[u];i++) printf("d %d %d %d\n",u,i,d[u][i]);
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=n-;i++){
u=read();v=read();ins(u,v);ind[v]++;
}
int root=-;
for(int i=;i<=n;i++) if(!ind[i]) {root=i;break;}
dfs(root);
int ans=INF;
for(int i=;i<=n;i++) if(size[i]>=m) ans=min(ans,d[i][m]+(i==root?:));
//,printf("ans %d %d\n",i,d[i][m]);
printf("%d",ans);
return ;
}
 

POJ1947 Rebuilding Roads[树形背包]的更多相关文章

  1. POJ1947 - Rebuilding Roads(树形DP)

    题目大意 给定一棵n个结点的树,问最少需要删除多少条边使得某棵子树的结点个数为p 题解 很经典的树形DP~~~直接上方程吧 dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v] ...

  2. [USACO2002][poj1947]Rebuilding Roads(树形dp)

    Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Descrip ...

  3. DP Intro - poj 1947 Rebuilding Roads(树形DP)

    版权声明:本文为博主原创文章,未经博主允许不得转载. Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  4. POJ 1947 Rebuilding Roads 树形DP

    Rebuilding Roads   Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...

  5. [poj 1947] Rebuilding Roads 树形DP

    Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10653 Accepted: 4884 Des ...

  6. POJ 1947 Rebuilding Roads 树形dp 难度:2

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9105   Accepted: 4122 ...

  7. POJ-1947 Rebuilding Roads (树形DP+分组背包)

    题目大意:将一棵n个节点的有根树,删掉一些边变成恰有m个节点的新树.求最少需要去掉几条边. 题目分析:定义状态dp(root,k)表示在以root为根节点的子树中,删掉一些边变成恰有k个节点的新树需要 ...

  8. POJ1947 Rebuilding Roads(树形DP)

    题目大概是给一棵树,问最少删几条边可以出现一个包含点数为p的连通块. 任何一个连通块都是某棵根属于连通块的子树的上面一部分,所以容易想到用树形DP解决: dp[u][k]表示以u为根的子树中,包含根的 ...

  9. POJ1947 Rebuilding Roads

    Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...

随机推荐

  1. Eclipse导入项目出现红色叹号的解决方法

    情景: 我在另一台电脑开发Java Web项目,开发环境为 JDK 1.7,Tomcat v7.0,然后导入另一台电脑上,开发环境为为 JDK 1.8,Tomcat v8.0. 问题: 导入项目出现红 ...

  2. python基础之数据类型(一)

    Python3 数字(Number) 定义:a=1 特性: 1.只能存放一个值 2.一经定义,不可更改 3.直接访问 分类:整型,长整型,布尔,浮点,复数 python2.*与python3.*关于整 ...

  3. CSS手动改变DIV高宽

    本实例代码可以使DIV可以手动改变大小 效果体验:http://hovertree.com/code/css/resize.htm 代码如下: <!DOCTYPE html> <ht ...

  4. 本地部署arcgis by eclipse

    首次来博客园发帖,从本地部署arcgis api开始吧: 首先还是下载arcgis的api包开始,在中国区官网下载arcgis包: 1.http://support.esrichina.com.cn/ ...

  5. jQuery插件之——简单日历

    最近在研究js插件的开发,以前看大神们,对插件都是信手拈来,随便玩弄,感觉自己要是达到那种水平就好了,就开始自己研究插件开发了.研究了一段时间之后,就开始写了自己的第一个日历插件,由于是初学插件开发, ...

  6. “不要抄代码!自己的代码也不要抄!”

    在 Adventure 位于深圳的电子设备组装厂(SZE)里,小朱狠狠的对自己说. 他刚刚在调试 STM32F407VG 的 SPI 功能.就在昨天,他刚刚调试好了 STM32F407VG 的 USA ...

  7. Android 手机卫士--对话初次设置密码验证过程

    本文实现设置密码对话框的逻辑判断 本文地址:http://www.cnblogs.com/wuyudong/p/5940551.html,转载请注明出处. 首先添加上图按钮的监听事件代码 /** * ...

  8. RubyMine不能调试Rails项目的问题

    需要安装debase gem,而且在项目的GemFile中禁用byebug

  9. python编码问题

    SCII编码是1个字节,而Unicode编码(汉字)通常是2个字节.一个字节8位(bit) 如果统一成Unicode编码,英文字母就会占用2个字节,造成空间浪费.从而出现了utf8可变编码,utf8编 ...

  10. 开发者调试工具Chrome Workspace

    Workspace是个什么样的东西呢?他能够在开发者工具中调试修改js或者css同时自动保存文件,能够避免开发人员在工具中调试好,再到编辑器中修改一次代码的重复操作,能够提高一定的效率 配置Chrom ...