POJ题目1947 Rebuilding Roads(树形dp)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9957 | Accepted: 4537 |
Description
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
* 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
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
Source
题目大意:问一个数删掉最少条边变成一个仅仅有n个结点的子树
ac代码
#include<stdio.h>
#include<string.h>
#define min(a,b) (a>b? b:a)
#define INF 0xfffffff
int dp[220][220];
int pre[220],head[220],vis[220],dig[220];
int n,p,cnt;
struct s
{
int u,v,w,next;
}edge[220*2];
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void tree_dp(int u)
{
int i,j,k;
for(i=0;i<=p;i++)
{
dp[u][i]=INF;
}
dp[u][1]=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
tree_dp(v);
for(k=p;k>=1;k--)
{
dp[u][k]=dp[u][k]+1;
for(j=1;j<k;j++)
{ dp[u][k]=min(dp[u][k],dp[u][j]+dp[v][k-j]);
}
}
}
}
int DP(int u)
{
tree_dp(u);
int ans=dp[u][p];
int i;
for(i=1;i<=n;i++)
{
ans=min(ans,dp[i][p]+1);
// printf("%d\n",dp[i][1]);
}
return ans;
}
int main()
{
//int n,p;
while(scanf("%d%d",&n,&p)!=EOF)
{
int i;
memset(dig,0,sizeof(dig));
memset(head,-1,sizeof(head));
cnt=0;
for(i=0;i<n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
dig[b]++;
}
int root;
for(i=1;i<=n;i++)
{
if(dig[i]==0)
root=i;
}
printf("%d\n",DP(root));
}
}
POJ题目1947 Rebuilding Roads(树形dp)的更多相关文章
- POJ 1947 Rebuilding Roads 树形DP
Rebuilding Roads Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...
- POJ 1947 Rebuilding Roads 树形dp 难度:2
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9105 Accepted: 4122 ...
- DP Intro - poj 1947 Rebuilding Roads(树形DP)
版权声明:本文为博主原创文章,未经博主允许不得转载. Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissi ...
- [poj 1947] Rebuilding Roads 树形DP
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10653 Accepted: 4884 Des ...
- POJ 1947 Rebuilding Road(树形DP)
Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...
- POJ1947 - Rebuilding Roads(树形DP)
题目大意 给定一棵n个结点的树,问最少需要删除多少条边使得某棵子树的结点个数为p 题解 很经典的树形DP~~~直接上方程吧 dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v] ...
- POJ 1947 Rebuilding Roads (树dp + 背包思想)
题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...
- 树形dp(poj 1947 Rebuilding Roads )
题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
随机推荐
- vagrant使用centos的环境安装..
vagrant这货挺好用的..简要就是, 下好virtualbox, vagrant, 然后下个你需要的box. 然后vagrant box add boxname boxpath就行. 然后在合适的 ...
- RabbitMQ~消费者实时与消息服务器保持通话
这个文章主要介绍简单的消费者的实现,rabbitMQ实现的消费者可以对消息服务器进行实时监听,当有消息(生产者把消息推到服务器上之后),消费者可以自动去消费它,这通常是开启一个进程去维护这个对话,它与 ...
- OpenAMP简介
通常在AMP(非对称多处理)配置中,会采用在不同的处理核上运行不同的软件环境并执行各自的代码程序,各核心之间通力合作实现处理器性能的提升.在AMP系统中,所谓的主处理器通常是指最先启动且主要负责管理其 ...
- Java编程思想读书笔记_第二章
java对于将一个较大作用域的变量“隐藏”的场景会有保护:编译告警.比如: int x = 5; { int x = 6; } 但是对于类中方法的局部变量和类成员变量确是可以重名的,比如 class ...
- ZUK Z2 Pro(Z2121) 免解锁BL 免rec Magisk Xposed ROOT 救砖 ZUI 4.0.247
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- C#入门经典 Chapter3 变量和表达式
3.1 C#基本语法 分号结束语句 花括号字符不需要附带分号 缩进 注释:/*....*/,//,/// 区分大小写 3.2 C#控制台应用程序的基本结构 namespace Chapter3 ...
- SQL基本操作——约束
我们将主要探讨以下几种约束: 1.NOT NULL 2.UNIQUE 3.PRIMARY KEY 4.FOREIGN KEY 5.CHECK 6.DEFAULT SQL NOTNULL约束:NOT N ...
- java攻城师之路--复习java web之Cookie_Session
Servlet技术 用来动态生成 网页数据资源Servlet生成HTML 页面数据时,所有内容都是通过 response.getWriter response.getOutputStream 向浏览器 ...
- 在PHP中调用php_ssh实现远程登陆linux服务器并执行shell脚本。
这个功能主要用于在web端利用程序对远程服务器进行操作,通过PHP_ssh执行shell脚本来实现. 首先要安装php_ssh2组件,linux中centos7下有ssh2源,直接安装.window下 ...
- EF入门
1.(安装EF)右键项目