树形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. Windows_7_休眠按钮没有了_如何找回?

    1. 在运行中输入:   powercfg -h on或者在命令行下输入:   powercfg.exe /hibernate on注意:执行这个命令需要管理员权限. “休眠”回来了   2. 还是没 ...

  2. Google的Java开发规范

    长期以来,Google一直有针对各种语言的规范,例如C++, Python等等.惟独对于Java语言,Google一直没有给出相应的规范(传说中是因为Google Java首席构架师Joshua Bl ...

  3. MOOCULUS微积分-2: 数列与级数学习笔记 5. Another comparison test

    此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...

  4. C# List根据某一字段排序 将字段相同的排序到一起

    List<JZJLXQ_Prescription_Item> ciList = new List<JZJLXQ_Prescription_Item>(); List<JZ ...

  5. PHP的单引号与双引号的区别

    字符串应用 单引号 较好! 在某些特定情况下,单引号的效率比双引号高. PHP把单引号中的数据视为普通字符串,不再处理. 而双引号还要对其中的字符串进行处理,比如遇到$了会把其后的内容视为变量等.

  6. SampleDateFormat进行日期格式化

    我们生成的日期 ,可能不是我们想要的格式,这时候,就要用到SampleDateFormat类的format方法转换一下, SampleDateFormat是java.text包下的一个常用日期类 这个 ...

  7. hdu 2010 - 水仙花数

    题意: 数学上有个水仙花数,他是这样定义的:"水仙花数"是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求输出所有在m和n范围内的水仙花 ...

  8. BZOJ4415: [Shoi2013]发牌

    显然可以线段树或树状数组上二分. 然而直接写splay在bzoj上并不会T. 然而发这题的目的只是因为我又忘了return了啊啊啊啊(TдT) 内心十分崩溃.关键是在本地还能过. #include&l ...

  9. BZOJ1804: [Ioi2007]Flood 洪水

    把点按坐标排序,每次找出最小的点,一定在最外层,再顺着把最外层的边删掉,经过了两次的边不会被冲毁. 其实不难写,但是写了很久. #include<bits/stdc++.h> #defin ...

  10. 一台机子上运行使用不同Java版本的多个tomcat

    方法 在tomcat/bin/下创建setenv.sh并写入 export JAVA_HOME=/usr/share/jvm/jdk1..0_91 When you starting tomcat u ...