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

    Rebuilding Roads

    Time Limit: 1000ms
    Memory Limit: 30000KB
    This problem will be judged on PKU. Original ID: 1947
    64-bit integer IO format: %lld      Java class name: Main
     
    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

     
    解题:树形dp
     
    dp[i][j]表示以i为根保留j个点最少需要去掉多少条边
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <vector>
     5 using namespace std;
     6 const int maxn = 200;
     7 int n,k,ret,dp[maxn][maxn],son[maxn];
     8 vector<int>g[maxn];
     9 int dfs(int u){
    10     son[u] = 1;
    11     for(int i = g[u].size()-1; i >= 0; --i) son[u] += dfs(g[u][i]);
    12     dp[u][1] = g[u].size();//u有g[u].size()个直系儿子 删除儿子还剩自己
    13     for(int i = g[u].size()-1; i >= 0; --i){
    14         for(int j = son[u]; j > 0; --j)
    15             for(int k = 1; k < j && k <= son[g[u][i]]; ++k)
    16                 dp[u][j] = min(dp[u][j],dp[u][j - k] + dp[g[u][i]][k] - 1);
    17     }
    18     if(son[u] >= k) ret = min(ret,dp[u][k] + (u != 1));
    19     return son[u];
    20 }
    21 int main(){
    22     int u,v;
    23     while(~scanf("%d%d",&n,&k)){
    24         for(int i = 0; i < maxn; ++i) g[i].clear();
    25         for(int i = 1; i < n; ++i){
    26             scanf("%d%d",&u,&v);
    27             g[u].push_back(v);
    28         }
    29         memset(dp,0x3f,sizeof dp);
    30         memset(&ret,0x3f,sizeof ret);
    31         dfs(1);
    32         printf("%d
    ",ret);
    33     }
    34     return 0;
    35 }
    View Code
  • 相关阅读:
    20155333 《网络对抗》 Exp6 信息搜集与漏洞扫描
    20155333 《网络对抗》 Exp5 MSF基础应用
    20155333 《网络对抗》Exp4 恶意代码分析
    20155333 《网络对抗》Exp3 免杀原理与实践
    20155333 《网络对抗》Exp2 后门原理与实践
    2017-2018-2 20155333 《网络对抗技术》 Exp1 PC平台逆向破解
    2017-2018-1 20155333 《信息安全系统设计基础》第三周学习总结
    内核模块实践实验报告
    Linux内核期末总结
    Linux内核期中
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4789893.html
Copyright © 2011-2022 走看看