zoukankan      html  css  js  c++  java
  • POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 11495   Accepted: 5276

    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


    题意:给定一棵节点数为n的树,问从这棵树最少删除几条边使得某棵子树的节点个数为p

    一开始想了个倒着选了几条边,其实正着也可以,先d[i][1]=子节点数量
    d[i][j]表示以i为根的子树节点数为j最少删几条边
    注意size要加上自己
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int N=155,INF=1e9;
    int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    int n,m,u,v,w,ind[N];
    struct edge{
        int v,w,ne;
    }e[N<<1];
    int h[N],cnt=0;
    void ins(int u,int v){
        cnt++;
        e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
    }
    int d[N][N],size[N];
    void dfs(int u){
        int child=0;size[u]=1;//!self
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v;
            dfs(v);
            size[u]+=size[v];
            child++;
        }
        if(!child) {size[u]=1;d[u][1]=0;return;}//printf("size %d %d
    ",u,size[u]);
        
        d[u][1]=child;
        for(int j=2;j<=size[u];j++) d[u][j]=INF;
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v;
            for(int j=size[u];j>=1;j--){
                int t=min(j-1,size[v]);
                for(int k=1;k<=t;k++) d[u][j]=min(d[u][j],d[u][j-k]+d[v][k]-1);
            }
        }
        
        //for(int i=1;i<=size[u];i++) printf("d %d %d %d
    ",u,i,d[u][i]);
    }
    int main(int argc, const char * argv[]) {
        n=read();m=read();
        for(int i=1;i<=n-1;i++){
            u=read();v=read();ins(u,v);ind[v]++;
        }
        int root=-1;
        for(int i=1;i<=n;i++) if(!ind[i]) {root=i;break;}
        dfs(root);
        int ans=INF;
        for(int i=1;i<=n;i++) if(size[i]>=m) ans=min(ans,d[i][m]+(i==root?0:1));
            //,printf("ans %d %d
    ",i,d[i][m]);
        printf("%d",ans);
        return 0;
    }
     
  • 相关阅读:
    linux 上安装sqlplus
    如何使用 SVN 找到一段时间内提交的代码文件
    nginx 快速检查配置文件的方法
    nginx 报错 [emerg] 1164#1664: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
    nginx 负载均衡
    C#可扩展编程之MEF学习
    5天玩转C#并行和多线程编程
    C#综合揭秘——深入分析委托与事件
    解析C#中[],List,Array,ArrayList的区别及应用
    在easyui datagrid中formatter数据后使用linkbutton
  • 原文地址:https://www.cnblogs.com/candy99/p/6072077.html
Copyright © 2011-2022 走看看