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

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.] 
 
题意:最小多少切割次数切割出一棵P节点的子树
思路:不要以为是切掉P个点...是切出
1 dp[i][j]节点i切成j的子树所需要的最小切数
2 有两种转移,第一种切断子树,需要+1,第二种合并子树
具体看代码,注意不要互相更新
错误5次:1 胡乱提交 2 互相更新 3 忘了非根子树要切
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=152;
const int inf=0x7ffff;
int dp[maxn][maxn];
int des[maxn];//中间缓存防止自身更新
int e[maxn][maxn];
int len[maxn];//建图
int lef[maxn];//子节点+自身个数
int n,p;
void dfs(int s){
lef[s]=1;//自身肯定算一个,子节点还没加上
dp[s][1]=0;//这个时候只有不切一种可能
if(len[s]==0){return ;}//没必要刻意 for(int i=0;i<len[s];i++){
int t=e[s][i];
dfs(t);
fill(des,des+n+1,inf);//初始化缓存
for(int k=1;k<=lef[s];k++){
des[k]=dp[s][k]+1;//切
}
for(int k=1;k<=lef[s];k++){
for(int j=1;j<=lef[t];j++){
des[k+j]=min(dp[s][k]+dp[t][j],des[k+j]);//不切
}
}
lef[s]+=lef[t];//加上这一枝
for(int k=1;k<=lef[s];k++){
dp[s][k]=des[k];//从缓存中取状态
}
dp[s][lef[s]]=0;//不需要
}
}
int main(){
scanf("%d%d",&n,&p);
memset(len,0,sizeof(len));
for(int i=1;i<=n;i++)fill(dp[i]+1,dp[i]+n+1,inf);
for(int i=2;i<=n;i++){
int f,t;
scanf("%d%d",&f,&t);
e[f][len[f]++]=t;
}
dfs(1);
int ans=dp[1][p];//1是根节点分离它不需要切
for(int i=2;i<=n;i++)ans=min(ans,dp[i][p]+1);//非根子树都要切
// printdp();
printf("%d\n",ans); return 0;
}

  

POJ 1947 Rebuilding Roads 树形dp 难度:2的更多相关文章

  1. POJ 1947 Rebuilding Roads 树形DP

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

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

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

  3. [poj 1947] Rebuilding Roads 树形DP

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

  4. POJ 1947 Rebuilding Road(树形DP)

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

  5. POJ 1947 Rebuilding Roads (树dp + 背包思想)

    题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...

  6. 树形dp(poj 1947 Rebuilding Roads )

    题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...

  7. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  8. POJ1947 - Rebuilding Roads(树形DP)

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

  9. POJ 1947 Rebuilding Roads(树形DP)

    题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...

随机推荐

  1. 在服务中用管理员权限创建一个可弹出UI的进程 (转载)

    转载:http://blog.csdn.net/woshinia/article/details/7850295 转载:http://blog.csdn.net/hurryboylqs/article ...

  2. BZOJ 1503 郁闷的出纳员(splay)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题意:给出一个数列(初始为空),给出一个最小值Min,当数列中的数字小于Min时自动 ...

  3. ubuntu 把软件源修改为国内源和更新

    1. 备份原始文件 sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup 2. 修改文件并添加国内源 vi /etc/apt/sourc ...

  4. Ubuntu 16.04设置IP、网关、DNS

    说明:在网上给的教程上面通常会有这样的一个误导思路,按照配置文件设置后会不生效的问题,甚至没有一点效果,经过排查发现Linux下设置IP这个话题的入口线索应该分为两种:1为Server版,2为Desk ...

  5. npm教程_脚手架原理以及bootstrap引入

    格式:vue init <templateName> <ProjectName> 例子:vue init webpack vue02 运行上面的命令后,脚手架帮忙按照webpa ...

  6. 深度优先搜索初尝试-DFS-LakeCounting POJ No.2386

    DFS入门的一道经典题目:LakeCounting 用栈或队列来实现: #include<cstdio> #include<stdlib.h> #include<iost ...

  7. 02_Kafka单节点实践

    1.实践场景 开始前的准备条件: 1) 确认各个节点的jdk版本,将jdk升级到和kafka配套的版本(解压既完成安装,修改/etc/profile下的JAVA_HOME,source /etc/pr ...

  8. LR下载及破解

    原文:http://chjuan1122.blog.163.com/blog/static/122892032013327111040128/ 地址:http://www.genilogix.com/ ...

  9. python 普通文件读写

    with open('ttt.txt', 'w') as f: f.write('456.098909,9.090988,7.878765') with open('ttt.txt', 'r') as ...

  10. Navicat+Premium+12+破解补丁

    链接:https://pan.baidu.com/s/1BsEWQ__X-RQPuw2ymfxhtg 提取码:j2kb