树形DP。。。。。
Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 8188 Accepted: 3659

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

现在设dp[j]表示以i为根的子树中节点个数为j的最少删除边数

状态转移方程: dp[1] = tot                                         (tot为他的子节点个数)

dp[j] = min(dp[j],dp[k]-1+dp[s][j-k])  (1<=i<=n,2<=j<=sum(节点总和),1<=k<j,s为i子节点)(i中已有k个节点并从s中选择j-k个,算最少删除边数,s选上所以i->s的边不需删除,所以-1)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int INF=0x3f3f3f3f;

int dp[200][200],sum[200],N,P;
vector<int> g[200];
bool vis[200];

void Tree_DP(int s)
{
    if(vis[s]) return;
    int tot = 0;
    vis[s]=true;sum[s]=1;
    for(int i=0;i<g[s].size();i++)
    {
        int v=g[s];
        if(vis[v]) continue;
        Tree_DP(v);
        sum[s]+=sum[v];
        tot++;
    }
    dp[s][1]=tot;
    for(int i=0;i<g[s].size();i++)
    {
        int v=g[s];
        for(int j=sum[s];j>=2;j--)
        {
            for(int k=1;k<j;k++)
            {
                if(dp[s][k]!=INF&&dp[v][j-k]!=INF)
                {
                    dp[s][j]=min(dp[s][j],dp[s][k]+dp[v][j-k]-1);
                }
            }
        }
    }
}

int main()
{
    while(scanf("%d%d",&N,&P)!=EOF)
    {
        for(int i=0;i<N+10;i++)
            g.clear();
        for(int i=0;i<N-1;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
            g.push_back(a);
        }
        memset(dp,63,sizeof(dp));
        memset(vis,false,sizeof(vis));
        memset(sum,0,sizeof(sum));
        Tree_DP(1);
        int ans=INF;
        for(int i=1;i<=N;i++)
        {
            if(i==1)
                ans=min(ans,dp[P]);
            else ans=min(dp[P]+1,ans);
        }
        printf("%d\n",ans);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 1947 Rebuilding Roads的更多相关文章

  1. [poj 1947] Rebuilding Roads 树形DP

    Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10653 Accepted: 4884 Des ...

  2. POJ 1947 Rebuilding Roads 树形DP

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

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

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

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

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

  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)

    题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...

  8. POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..

    dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t)  < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...

  9. DP Intro - poj 1947 Rebuilding Roads

    算法: dp[i][j]表示以i为根的子树要变成有j个节点的状态需要减掉的边数. 考虑状态转移的时候不考虑i的父亲节点,就当不存在.最后统计最少减去边数的 时候+1. 考虑一个节点时,有两种选择,要么 ...

随机推荐

  1. android 事件传递机制 心得

    看了网上很多资料. 最后我发现可以用很简单的几句话就能把它说清楚 1 每个 viewgroup 内都有 三个方法 a dispatchTouchEvent 是自己决定要不要(管他爹)要这个苹果的 一般 ...

  2. list去除并且把值相加

    package list; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import ja ...

  3. 伪集群zookeeper模式下codis的部署安装

    1,zookeeper伪集群部署     部署在192.168.0.210服务器上          下载     去官网将3.4.6版本的zookeeper下载下来到/app目录下解压     首先 ...

  4. 用DOS命令配置服务开机自启动

    2016-08-19 15:01 Create 使用命令  sc  config 参考博客:http://blog.csdn.net/it1988888/article/details/7992626 ...

  5. 逐行读取txt文件

    header("Content-type:text/html; charset=utf-8"); $handle = fopen('test.php', 'r') or die(' ...

  6. boolean 和 Boolean 类型数据的差别

    工作中遇到页面传递布尔类型的数据问题,需要在代码中判断不能为null,就此思考一下,boolean和Boolean之间的区别?boolean是基本数据类型Boolean是它的封装类,和其他类一样,有属 ...

  7. cmd chcp命令切换字符格式UTF8

    cmd chcp命令切换字符格式   命令介绍:   chcp 65001   #换成utf-8代码页   chcp 936       #换成默认的gbk   chcp 437       #美国英 ...

  8. Yoshua Bengio 2016年5月11日在Twitter Boston的演讲PPT

    Yoshua Bengio最新演讲:Attention 让深度学习取得巨大成功(46ppt) Yoshua Bengio,电脑科学家,毕业于麦吉尔大学,在MIT和AT&T贝尔实验室做过博士后研 ...

  9. 数字图像处理- 3.4 空间滤波 and 3.5 平滑空间滤波器

    3.4 空间滤波基础 • Images are often corrupted by random variations in intensity, illumination, or have poo ...

  10. jQM基本代码

    <div data-role="page"> <div data-role="header" data-position="fixe ...