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

正解:树形DP

解题报告:

  感觉自己DP很萎,最近练一下DP。

  f[i][j]记录以i为根结点的子树切出j个节点的最小代价,转移的话也很简单,f[x][j] = min{f[x][j],f[son[x][i]][k]+f[x][j-k]-2},减2是因为要减去算了两次的x到son[x][i]的那条边。

  递归转移就可以了。

  题解传送门:http://www.cnblogs.com/celia01/archive/2012/08/02/2619063.html

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = ;
int f[MAXN][MAXN];//f[i][j]记录以i为根结点的子树切出j个节点的最小代价
int du[MAXN];
vector<int>w[MAXN];
int n,p;
int ans;
int root; inline int getint(){
char c=getchar(); int w=,q=;
while(c!='-' && ( c<'' || c>'')) c=getchar();
if(c=='-') c=getchar(),q=;
while(c>='' && c<='') w=w*+c-'',c=getchar();
return q?-w:w;
} inline void dfs(int x){
if(x==root) f[x][]=w[x].size();
else f[x][]=w[x].size()+;//父边 for(int i=;i<w[x].size();i++) dfs(w[x][i]); for(int i=;i<w[x].size();i++)
for(int j=p;j>=;j--)
for(int k=;k<=j;k++) {
if(f[x][k]!=MAXN && f[w[x][i]][j-k]) {
f[x][j]=min(f[x][j],f[x][k]+f[w[x][i]][j-k]-);
}
}
} int main()
{
n=getint(); p=getint(); int x,y;
for(int i=;i<n;i++) {
x=getint(); y=getint();
w[x].push_back(y); du[y]++;
} for(int i=;i<=n;i++) for(int j=;j<=n;j++) f[i][j]=MAXN; for(int i=;i<=n;i++) if(du[i]==) { root=i; dfs(i); break; } ans=MAXN;
for(int i=;i<=n;i++) ans=min(ans,f[i][p]); printf("%d",ans); return ;
}

POJ1947 Rebuilding Roads的更多相关文章

  1. POJ1947 Rebuilding Roads[树形背包]

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

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

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

  3. POJ1947 Rebuilding Roads(树形DP)

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

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

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

  5. POJ1947 - Rebuilding Roads(树形DP)

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

  6. 【树形dp】Rebuilding Roads

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

  7. POJ 1947 Rebuilding Roads

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

  8. POJ 1947 Rebuilding Roads 树形DP

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

  9. [poj 1947] Rebuilding Roads 树形DP

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

随机推荐

  1. 利用OpacityMask制作打洞效果

    起因 项目上存在一个连线功能,在设计的原型中,在连线中间文字上下各有15像素的空白.接手的同事觉得没思路,问我能不能在不影响连线后面的背景情况下解决该问题.我就抽了点时间给他写了个Demo.回家后趁热 ...

  2. github结合TortoiseGit使用sshkey,无需输入账号和密码

    1.github上支持三种方式进行项目的clone    https,ssh,subversion https://github.com/用户名/版本库.git ssh的方式 git@github.c ...

  3. [转] Centos 6.4 python 2.6 升级到 2.7

    http://blog.csdn.net/jcjc918/article/details/11022345

  4. page-cache层以及各种标志位之间的转换

    对真实文件系统层,算是懂了,但是vfs层以及block层还是有点生疏呢,最近要好好分析一下了. page-cache层主要关注文件读写时的行为,包括页的状态之间的变化,何时变脏,何时变成writeba ...

  5. Http请求中POST与GET的区别——前端面试

    一.原理区别 Http定义了与服务器交互的方法,其中最基本的四种是:GET,POST,PUT,DELETE,正对应着对资源的查,改,增,删.URL的全称是资源描述符,我们可以这样认为,一个URL地址, ...

  6. 怎么写针对IE9的CSS

    (自己亲自试过有用)针对IE9的CSS只需在相应CSS代码加入只有IE9识别的 \9\0.具体代码如下: .div{ background-color:#0f0\9\0;/* ie9 */ } 其他浏 ...

  7. 访问 IIS 元数据库失败 的解决方法

    系统是windows xp sp3,vs2010.安装了iis 5.1 ,创建了虚拟目录之后copy进去一个web sercices测试下能不能正常工作. 出现如下问题: 访问 IIS 元数据库失败. ...

  8. [MetaHook] Load large texture from model

    We need hook "GL_LoadTexture" engine function. GL_LOADTEXTURE_SIG from hw.dll(3266) engine ...

  9. Week2学习过程报告

    一.学习内容 1. 熟悉Linux系统下的开发环境   2. 熟悉vi的基本操作   3. 熟悉gcc编译器的基本原理   4. 熟练使用gcc编译器的常用选项   5 .熟练使用gdb调试技术    ...

  10. 20145208 《Java程序设计》第一周学习总结

    20145208 <Java程序设计>第X周学习总结 教材学习内容总结 这几天我学习java的基础内容,这几天我学习了java的基础内容,从教材上面我了解到了java是一种程序语言,但他又 ...