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. javascript中的array对象属性及方法

    Array 对象 Array 对象用于在单个的变量中存储多个值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, e ...

  2. 第二章 下山遇虎(@helper)

    @helper方法定义 使用@helper关键字可以定义一个方法,这样就可以在页面中调 用这个方法了,和C#中的方法一样.在页面中定义的方法可以访问ViewBag,HttpContext等等页面的属性 ...

  3. MVC3 使用 FusionCharts 做报表

    环境 VS2010+SQL2008 +MVC3 报表思路 1.新建报表需要的数据表,FusionCharts将直接获取数据表的数据进行展示 2.使用SQL代理,通过存储过程定时生成数据表的数据,只添加 ...

  4. CardboardCamera Prefab 中文笔记

    在Cardboard的预制体(Prefab)中, CardboardCamera是最简单的一个,仅有两个子物体,一个PostRender, 一个PreRender,以及分别带的Camera组件. Ca ...

  5. 静态时序分析(static timing analysis) --- 时序路径

    时序分析工具会找到且分析设计中的所有路径.每一个路径有一个起点(startpoint)和一个终点(endpoint).起点是设计中数据被时钟沿载入的那个时间点,而终点则是数据通过了组合逻辑被另一个时间 ...

  6. C#委托Action、Action<T>、Func<T>、Predicate<T>

    CLR环境中给我们内置了几个常用委托Action. Action<T>.Func<T>.Predicate<T>,一般我们要用到委托的时候,尽量不要自己再定义一 个 ...

  7. scrapy 的三个入门应用场景

    说明: 本文参照了官网的 dmoz 爬虫例子. 不过这个例子有些年头了,而 dmoz.org 的网页结构已经不同以前.所以我对xpath也相应地进行了修改. 概要: 本文提出了scrapy 的三个入门 ...

  8. CSS 实现加载动画之三-钢琴按键

    今天做的这个动画实现也是非常简单,简单数几行代码就可以搞定.给这个动画取了个优雅的名字--钢琴按键,也实在是想不出什么更形象的名字了.废话不多说,直接上图. 1. 先看gif图 2. 代码实现思路 2 ...

  9. Caffe学习系列(6):Blob,Layer and Net以及对应配置文件的编写

    深度网络(net)是一个组合模型,它由许多相互连接的层(layers)组合而成.Caffe就是组建深度网络的这样一种工具,它按照一定的策略,一层一层的搭建出自己的模型.它将所有的信息数据定义为blob ...

  10. 在matlab中进行遥感影像地理坐标的相互转换

    在matlab中进行图像处理,一般使用的都是图像本地坐标,以左上角(1,1)开始.处理完成后,如果要将结果在带地理坐标的遥感影像中显示,或者需要输出成shp文件,就需要涉及到本地坐标和地理坐标的转换, ...