zoukankan      html  css  js  c++  java
  • [POJ 1947] Rebuilding Roads

    Rebuilding Roads
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 9249   Accepted: 4198

    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
    树形背包、- -
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    #define INF 0x7ffffff
    #define N 155
    
    struct Edge
    {
        int to,next;
    }edge[N*N/2];
    int head[N],tot;
    
    int n,m;
    int f[N];
    int dp[N][N];
    
    void add(int x,int y)
    {
        edge[tot].to=y;
        edge[tot].next=head[x];
        head[x]=tot++;
    }
    
    void dfs(int u)
    {
        int i,j,k;
        for(j=0;j<=m;j++)
        {
            dp[u][j]=INF;
        }
        dp[u][1]=0;
        for(i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            dfs(v);
            for(j=m;j>=0;j--)
            {
                for(k=0;k<j;k++)
                {
                    if(k)
                        dp[u][j] = min(dp[u][j],dp[u][j-k]+dp[v][k]);
                    else
                        dp[u][j] = dp[u][j]+1;
                }
            }
        }
    }
    
    void init()
    {
        tot=0;
        memset(f,-1,sizeof(f));
        memset(head,-1,sizeof(head));
    }
    int main()
    {
        int i;
        while(scanf("%d%d",&n,&m), n||m)
        {
            init();
            for(i=1;i<n;i++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                add(a,b);
                f[b]=a;
            }
            int root=1;
            while(f[root]!=-1)
            {
                root=f[root];
            }
            dfs(root);
            int ans=dp[root][m];
            for(i=1;i<=n;i++)              //除了根节点,其他节点要想成为独立的根,必先与父节点断绝关系,所以要先加1
            {
                ans=min(ans,dp[i][m]+1);
            }
            printf("%d
    ",ans);
        }    
        return 0;
    }
    趁着还有梦想、将AC进行到底~~~by 452181625
  • 相关阅读:
    Python基本语法_输入/输出语句详解
    集成骨骼动画Spine的几点经验
    标量 ,数组,hash 引用
    阿里RDS中插入emoji 表情插入失败的解决方案
    Target runtime Apache Tomcat v7.0 is not defined.
    销售行业ERP数据统计分析都有哪些维度?
    mysql 基于时间点恢复
    perl 闭包
    房地产企业营销分析系统建设中的关键性指标是什么?
    Python基本语法_运算符详解
  • 原文地址:https://www.cnblogs.com/hate13/p/4095561.html
Copyright © 2011-2022 走看看