[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中代理有关的知识点都在这了。

    对于每一个Java开发来说,代理这个词或多或少都会听说过.你可能听到过的有代理模式.动态代理.反向代理等.那么,到底什么是代理,这么多代理又有什么区别呢.本文就来简要分析一下. 代理技术,其实不只是J ...

  2. word 宏,脚本编程

    '脚本方式新建word 再新建文档,文档中输入字符串"你好" Dim wdapp As Word.Application Dim wddoc As Word.Document Se ...

  3. hdu 1598 find the most comfortable road (并查集+枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...

  4. php中使用static方法

    <?php class Char{ public static $number = 0; public static $name; function __construct($what){ se ...

  5. Shell脚本 - nginx启动脚本

    OS:CentOS/Redhat 系列 并在 Centos 6.7 和 Centos 7.2 上测试正常 #!/bin/bash # # auth: daxin # time: 2018/07/10 ...

  6. Android IPC

    1. 什么是Android IPC IPC:inter-process Commnication跨进程的通信,多进程之间的通信,不同的操作系统有不同的通信方式,Android继承自Linux,但其IP ...

  7. Hadoop-MR[会用]MR程序的运行模式

    1.简介 现在很少用到使用MR计算框架来实现功能,通常的做法是使用hive等工具辅助完成.但是对于其底层MR的原理还是有必要做一些了解. 2.MR客户端程序实现套路 这一小节总结归纳编写mr客户端程序 ...

  8. python接口自动化13-data和json参数傻傻分不清【转载】

    转至博客:上海-悠悠 前言 在发post请求的时候,有时候body部分要传data参数,有时候body部分又要传json参数,那么问题来了:到底什么时候该传json,什么时候该传data? 一.识别j ...

  9. Javascript备忘录-枚举一个对象的所有属

    for...in 循环 var obj = { age: 18, fname: "Rand ", lname: "McKinnon" }; function s ...

  10. macos不能打开windows samba共享问题(转载)

    转自:https://www.macx.cn/thread-2095377-1-1.html?mod=viewthread&tid=2095377&extra=page%253D1&a ...