题意:给出一颗树,求要形成一颗元素个数是p的子树,最少要去掉多少边

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#define M 222
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int u,v,next;
}edge[M*2];
int t,head[M],dp[M][M],num[M],degree[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
void dfs(int u,int f)//对于每个点的子树处理
{
num[u]=1;
dp[u][1]=degree[u];//对于每个点只取一个点
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==f)continue;
dfs(v,u);
num[u]+=num[v];
for(int j=num[u];j>=1;j--)
{
for(int k=1;k+1<=j;k++)
dp[u][j]=min(dp[u][j],dp[u][k]+dp[v][j-k]-2);
}
}
}
int main()
{
int n,p,i;
while(scanf("%d%d",&n,&p)!=-1)
{
init();
memset(degree,0,sizeof(degree));
for(i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
degree[a]++;
degree[b]++;
}
memset(dp,inf,sizeof(dp));
dfs(1,1);
int ans=inf;
for(i=1;i<=n;i++)
{
if(ans>dp[i][p])
ans=dp[i][p];
}
printf("%d\n",ans);
}
}

树形DP(Rebuilding Roads poj1947)的更多相关文章

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

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

  2. POJ1947 Rebuilding Roads(树形DP)

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

  3. POJ1947 - Rebuilding Roads(树形DP)

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

  4. 【树形dp】Rebuilding Roads

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

  5. POJ 1947 Rebuilding Roads 树形DP

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

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

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

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

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

  8. 树形dp(poj 1947 Rebuilding Roads )

    题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...

  9. POJ题目1947 Rebuilding Roads(树形dp)

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9957   Accepted: 4537 ...

随机推荐

  1. 【php学习】图片操作

    前两天要对一张图片进行处理,其实很简单,就是在图片上加上字符串,一个图片而已,但是自己如同得了短暂性失忆似的,图片操作的函数一个都想不起来.所以就抽空整理了一下图片操作函数. 图片处理三步走: 创建画 ...

  2. SQL Server存储机制二

    http://blog.csdn.net/ltylove2007/article/details/21084585 http://www.cnblogs.com/anding/p/3254674.ht ...

  3. php---apache的A/B测试

    A/B测试 A/B测试是一种新兴的网页优化方法,可以用于增加转化率注册率等网页指标..A/B测试的目的在于通过科学的实验设计.采样样本代表性.流量分割与小流量测试等方式来获得具有代表性的实验结论,并确 ...

  4. background:transparent的作用

    background的属性值 background : background-color | background-image | background-repeat | background-att ...

  5. [LeetCode] Scramble String(树的问题最易用递归)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  6. How to Move Magento to a New Server or Domain

    Magento is one of the fastest growing eCommerce platforms on the internet and with its recent acquis ...

  7. Round and Round We Go

    http://acm.hdu.edu.cn/showproblem.php?pid=1313 考查大整数与小整数相乘 #include<iostream> #include<cstd ...

  8. 10 Ways to Inspire Your Team

    Inspire. Just the word itself causes us to pause and think. We may remember our own personal heroes ...

  9. Windows-001-Win7系统变量设置

    本节主要讲述Windows系统环境变量配置介绍,以 Windows 7 为例讲解. 1.右键单击 计算机,选择 属性,进入 系统 界面,如下所示: 2.点击上图中的 高级系统设置,进入 系统属性 界面 ...

  10. iis7.5应用程序池模板永久性缓存初始化失败解决方法

    错误: 针对应用程序池的模板永久性缓存初始化失败,这是由以下错误导致的: 无法为应用程序池创建磁盘缓存子目录.数据可能包含其他错误代码. 解决办法如下: 网上搜索的答案全都是修改3个目录的权限,给II ...