[POJ1947]Rebuilding Roads
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11934   Accepted: 5519

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?
直接写不就好了么?
代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,M;
vector<int> vec[201];
int dp[201][201];
int ans=INF; void dfs(int x,int fa){
int cnt=0;
for(int i=0;i<vec[x].size();i++){
if(vec[x][i]!=fa)
dfs(vec[x][i],x),cnt++;
}
dp[x][1]=dp[x][0]=0;
for(int i=0;i<vec[x].size();i++){
if(vec[x][i]!=fa)
for(int j=M;j>=1;j--){
if(dp[x][j]!=INF) dp[x][j]++;
for(int k=1;k<=M;k++){
if(k>=j||dp[vec[x][i]][k]==INF) break;
if(dp[x][j-k]!=INF) dp[x][j]=min(dp[vec[x][i]][k]+dp[x][j-k],dp[x][j]);
}
}
}
if(x!=1) ans=min(ans,dp[x][M]+1);
else ans=min(ans,dp[x][M]);
return ;
} int main(){
N=read(),M=read();
for(int i=0;i<=N;i++)
for(int j=0;j<=M;j++) dp[i][j]=INF;
for(int i=1;i<N;i++){
int u=read(),v=read();
vec[u].push_back(v);
vec[v].push_back(u);
}
dp[1][1]=0;
dfs(1,-1);
if(ans!=INF) printf("%d\n",ans);
else puts("0");
}
//dp[i][j]表示i号节点的子树中隔离成为大小为j个的道路数量
//dp[i][k]=min(dp[i->son][j]+dp[i][k-j])

【树形dp】Rebuilding Roads的更多相关文章

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

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

  2. POJ 1947 Rebuilding Roads 树形DP

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

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

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

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

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

  5. 树形dp(poj 1947 Rebuilding Roads )

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

  6. POJ题目1947 Rebuilding Roads(树形dp)

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9957   Accepted: 4537 ...

  7. POJ1947 Rebuilding Roads(树形DP)

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

  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

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

随机推荐

  1. 关于反序列化时抛出java.io.EOFException异常

    https://www.cnblogs.com/ouhaitao/p/7683568.html https://blog.csdn.net/mym43210/article/details/40081 ...

  2. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛) F.猴子排序的期望

    题目链接:https://www.nowcoder.com/acm/contest/116/F 题目描述 我们知道有一种神奇的排序方法叫做猴子排序,就是把待排序的数字写在卡片上,然后让猴子把卡片扔在空 ...

  3. 2017ACM暑期多校联合训练 - Team 1 1002 HDU 6034 Balala Power! (字符串处理)

    题目链接 Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He ...

  4. hdu 1162 Eddy's picture(最小生成树算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...

  5. base--AuditResult

    //参考base-4.0.2.jar public class AuditResult implements TimeReferable, Serializable //参考api-1.0.0.jar ...

  6. Java8的Lambda表达式简介

    先阐述一下JSR(Java Specification Requests)规范,即Java语言的规范提案.是向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求 ...

  7. php中的base64写shell

    <?php system(base64_decode($_GET['info'])); #http://localhost/1.php?info=d2hvYW1p #这只是一个例子 ?>

  8. linux非阻塞的socket EAGAIN的错误处理【转】

    转自:http://blog.csdn.net/tianmohust/article/details/8691644 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux中使用非阻塞的s ...

  9. Tabular DataStream protocol 协议

    Tabular DataStream protocol 协议 Freetds 创建过程 https://wenku.baidu.com/view/2076cbfaaef8941ea76e0576.ht ...

  10. 81.Search in Rotated Sorted Array II---二分变形

    题目链接 题目大意:与33题类似,只是这里数组中有重复数值. 法一:解法与33题类似,只是这里要处理1,3,1,1,1这种情况,即有重复值时,mid与left和right都相等时,可以采用right- ...