Rebuilding Roads

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 10653 Accepted: 4884

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

USACO 2002 February

题目链接

http://poj.org/problem?id=1947

题意

给你一棵节点为n的树。问至少砍几刀能够孤立出一棵节点为m的子树。

思路

找到根节点(入度为0)后dfs在每一个节点统计dp[i][j]

表示i及其的子树保留j个节点的最小代价;

int tmp=dp[x][ii]+1;//不要y节点;
tmp=min(tmp,dp[x][ii-j]+dp[y][j]);
dp[x][ii]=tmp;

y为x的儿子节点。

代码

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
using namespace std; int dp[155][155];
int ans;
int in[155];
vector<int> lin[155];
int n,aa,bb,p;
void dfs(int x)
{
for(int i=2;i<=p;i++) dp[x][i]=99999999;
if(lin[x].size()==0)
dp[x][1]=0;
for(int i=0;i<lin[x].size();i++)
{
int y=lin[x][i];
dfs(y);
for(int ii=p;ii>=1;ii--)
{
int tmp=dp[x][ii]+1;
for(int j=1;j<ii;j++)
tmp=min(tmp,dp[x][ii-j]+dp[y][j]);
dp[x][ii]=tmp;
}
}
}
int main()
{
scanf("%d%d",&n,&p);
{
int ans=99999999;
for(int i=1;i<n;i++)
{
scanf("%d%d",&aa,&bb);
lin[aa].push_back(bb);
in[bb]++;
}
for(int i=1;i<=n;i++)
if(!in[i])
{
dfs(i);
ans=dp[i][p];
break;
} for(int i=1;i<=n;i++)
ans=min(ans,dp[i][p]+1);
printf("%d\n",ans);
} }

[poj 1947] Rebuilding Roads 树形DP的更多相关文章

  1. POJ 1947 Rebuilding Roads 树形DP

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

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

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

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

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

  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. DIOCP3-DIOCP1升级到DIOCP3

    DIOCP3兼容DIOCP1的,有些属性做了修改 DIOCP3, uIOCPConsole没有了, uMemPool没有了 1.DIOCP1,代码:   DIOCP3中去掉TIOCPContextFa ...

  2. 第二章 使用接口(Using Interfaces)-书籍翻译

    PDF预览 下载地址 http://files.cnblogs.com/DKSoft/CodingInDelphi.pdf 1.1. 解耦(Decoupling) All through this b ...

  3. SVN文件加锁

    原文:SVN与TortoiseSVN实战:文件加锁详解 加锁与解锁的操作对于项目中的二进制文件,如图片.声音.动态库等不可合并文件是非常有用的,可以让这些文件防止产生恼人的冲突,但TortoiseSV ...

  4. 解密OpenTSDB的表存储优化【转】

    https://yq.aliyun.com/articles/54785 摘要: 本篇文章会详细讲解OpenTSDB的表结构设计,在理解它的表结构设计的同时,分析其采取该设计的深层次原因以及优缺点.它 ...

  5. android 模拟器 sdcard权限修改

    http://blog.csdn.net/zj510/article/details/8645777 mksdcard 1024M c:\sdcard.img (路径随意,大小随意) 执行:adb p ...

  6. Nginx缓存功能、防盗链、URL重写

    nginx做为反向代理时,能够将来自upstream的响应缓存至本地,并在后续的客户端请求同样内容时直接从本地构造响应报文. nginx的缓存数据结构: 共享内存:存储键和缓存对象元数据 磁盘空间:存 ...

  7. spring 过滤器

    Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所当然地实现了javax.servlet.Filter,不过实现的方式有以下几类 ...

  8. Android开发(一)——全屏或者取消标题栏

    先介绍去掉标题栏的方法: 第一种:也一般入门的时候经常使用的一种方法 requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 注意这句一定要写在se ...

  9. Wireshark抓包工具HttpAnalyzerStdV7

    http.request.full_uri contains "XXXserver/api/" http.host contains "XXX5.单位.com"

  10. 5. 集成学习(Ensemble Learning)GBDT

    1. 集成学习(Ensemble Learning)原理 2. 集成学习(Ensemble Learning)Bagging 3. 集成学习(Ensemble Learning)随机森林(Random ...