zoukankan      html  css  js  c++  java
  • POJ 2378 树状dp

    Tree Cutting
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 3849   Accepted: 2304

    Description

    After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses.

    Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ.

    Please help Bessie determine all of the barns that would be suitable to disconnect.

    Input

    * Line 1: A single integer, N. The barns are numbered 1..N.

    * Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.

    Output

    * Lines 1..?: Each line contains a single integer, the number (from 1..N) of a barn whose removal splits the network into pieces each having at most half the original number of barns. Output the barns in increasing numerical order. If there are no suitable barns, the output should be a single line containing the word "NONE".

    Sample Input

    10
    1 2
    2 3
    3 4
    4 5
    6 7
    7 8
    8 9
    9 10
    3 8

    Sample Output

    3
    8

    Hint

    INPUT DETAILS:

    The set of connections in the input describes a "tree": it connects all the barns together and contains no cycles.

    OUTPUT DETAILS:

    If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).

    Source

     

    POJ 2378

    题目意思:

    在一棵树上去掉一些点,使得这棵树可以变成一些包含的顶点数目不超过原来的树的一般的一些树,求需要删除的节点,如果有多个节点,按照升序输出;


    解题思路:

    对树进行宽度优先遍历,如果一个结点的所有以这个节点的子节点为根的树的节点数不超过n/2,并且n减去以这个节点为根节点的树的节点个数小于等于n/2,则这个节点为可以删除的节点;

     

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<vector>
     7 using namespace std;
     8 const int maxn=10007;
     9 int dp[maxn];
    10 bool input[maxn];
    11 vector<int> G[maxn];
    12 int n;
    13 void init()
    14 {
    15     memset(dp,0,sizeof(dp));
    16     memset(input,0,sizeof(input));
    17     for(int i=0;i<maxn;i++)
    18         G[i].clear();
    19 }
    20 void bfs(int point,int father)
    21 {
    22     dp[point]=1;
    23     bool flag=true;int num=0;
    24     for(int i=0;i<G[point].size();i++)
    25     {
    26         int v=G[point][i];
    27         if(v==father) continue;
    28         bfs(v,point);
    29         if(dp[v]>n/2) flag=false;
    30         dp[point]+=dp[v];
    31     }
    32    if(flag&&n-dp[point]<=n/2)input[point]=true;
    33 }
    34 int main()
    35 {
    36    //freopen("in.txt","r",stdin);
    37     int a,b,root;
    38     while(~scanf("%d",&n)){
    39         init();root=-1;
    40         for(int i=1;i<n;i++)
    41         {
    42             scanf("%d %d",&a,&b);
    43             G[a].push_back(b);
    44             G[b].push_back(a);
    45         }
    46         bfs(1,-1);
    47         for(int i=1;i<=n;i++)  if(input[i]) printf("%d
    ",i);
    48     }
    49     return 0;
    50 }


  • 相关阅读:
    理解C语言中指针常量和常量指针区别!不要再搞混了~
    哪座城市可以安放程序员的灵魂,一线城市与二三线城市该如何择别?
    Linux 之父如何定义 "Linux" !主要想让黑客、计算机学生使用,学习和享受!
    程序员的凡尔赛文学!作为低调人群的程序员,“凡”起来又是怎样的一番景象呢?
    40个Java集合面试问题和答案
    从关系型数据库到非关系型数据库
    redis安装报错
    redis简介
    不满足依赖关系
    EL表达式中引用隐式变量
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4312208.html
Copyright © 2011-2022 走看看