POJ 1947 Rebuilding Roads(树形DP)
题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树。
思路 : dp[i][j]代表的是以i为根的子树有j个节点。dp[u][i] = dp[u][j]+dp[son][i-j]-1,son是u的儿子节点。初始是将所有的儿子都断开,然后-1代表的是这个儿子我需要了,不断了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define maxn 101 using namespace std; struct node
{
int fa,son,next ;
} pp[];
int head[],cnt,root[],root1,dp[][] ;
int p ;
int so[];//统计儿子的个数
void dfs(int u)
{
for(int i = ; i <= p ; i++)
dp[u][i] = ;
dp[u][] = so[u] ;
for(int ii = head[u] ; ii != - ; ii = pp[ii].next)
{
int son = pp[ii].son ;
dfs(son) ;
for(int i = p ; i >= ; i--)
{
int s = dp[u][i] ;
for(int j = ; j < i ; j++)
{
s = min(s,dp[u][j]+dp[son][i-j]-) ;
}
dp[u][i] = s ;
}
}
}
void addedge(int u,int v)
{
pp[cnt].fa = u ;
pp[cnt].son = v ;
pp[cnt].next = head[u] ;
head[u] = cnt ++ ;
}
int main()
{
int n,u,v;
while(~scanf("%d %d",&n,&p))
{
cnt = ;
memset(head,-,sizeof(head)) ;
memset(root,,sizeof(root)) ;
for(int i = ; i < n ; i++)
{
scanf("%d %d",&u,&v) ;
addedge(u,v) ;
so[u]++;
root[v] = ;
}
for(int i = ; i <= n ; i++)
if(root[i])
{
root1 = i ;//根节点
break ;
}
dfs(root1) ;
int ans = dp[root1][p] ;
for(int i = ; i <= n ; i++)
ans = min(ans,dp[i][p]+) ;
printf("%d\n",ans) ;
}
return ;
}
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 ...
- 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: ...
- POJ1947 - Rebuilding Roads(树形DP)
题目大意 给定一棵n个结点的树,问最少需要删除多少条边使得某棵子树的结点个数为p 题解 很经典的树形DP~~~直接上方程吧 dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v] ...
随机推荐
- 随机数范围扩展(如rand7()到rand10())(转)
题目:已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10() 随机1~10.分析:要保证rand10()在整数1-10的均匀分布,可以构造一个1-10*n的均 ...
- 如何检测某IP端口是否打开
1.如果你直接到控制面板的管理工具里的服务项里去找telnet的话,那是徒劳无功 的,因为默认根本就没有这一服务.当然,你可以通过如下方式搞定.“控制面 板” 一〉“程序” 一〉“打开或关闭windo ...
- Oracle 12c 数据库中scott用户不存在的解决方法
-- 使用超级管理员登录CONN sys/change_on_install AS SYSDBA ;-- 创建c##scott用户CREATE USER c##scott IDENTIFIED BY ...
- ubuntu 修改ssh远程主机名称,mac开机运行命令,静默方式启动virtual box虚拟机,静默执行run脚本
一.修改主机名 ssh登陆 vi /etc/hostname vi /etc/hosts hostname ulocal (执行这个命令,无须重启服务器) 保证127.0.0.1 的hostname与 ...
- Swift 中使用Nimble 库进行单元测试
Nimble 从字面上看是 敏捷,灵活 的意思.Nimble 是一个库,一个 断言库.这个库一般用于单元测试.Xcode 6 为我们集成了 XCTest 单元测试库.在正式介绍 Nimble 之前,我 ...
- 33.allegro中Autosilk top, Silkscreen top 和Assembly top三个什么区别(转)
Autosilk top, Silkscreen top 和Assembly top Autosilk top:最后出gerber的时候,自动生成的丝印层.会自动调整丝印位置,以及碰到阻焊开窗的地方, ...
- InputStream和OutputStream与String之间的转换
//1.字符串转inputstream String str="aaaaa"; InputStream in = new ByteArrayInputStream(str.getB ...
- 团队开发——Alpha版总结会议
本组目前存在的问题: 1.在选题的时候,题目选的比较有难度,造成后期工作量较大,实现有难度(未能正确估计项目的难度). 2.最初规划时,设计的功能较多,但是技术水平达不到,导致目前完成功能较少. 3. ...
- JS 面向对象之继承 -- 原型链
ECMAScript只支持实现继承,其实现继承主要是靠原型链来实现. 原型链的基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法. 简单回顾下构造函数.原型和实例的关系: 每个构造函数都有 ...
- FB接口之 js调用支付窗口
官方文档: https://developers.facebook.com/docs/reference/dialogs/pay/ <html xmlns="http://www.w3 ...