zoukankan      html  css  js  c++  java
  • Tree Cutting POJ

    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).
    数据比较水,求树的重心,固定套路
     1 #include<queue>
     2 #include<vector>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<iostream>
     6 #include<algorithm>
     7 using namespace std;
     8 
     9 const int maxn=1e4+5;
    10 
    11 struct node{
    12     int to,next;
    13 }e[4*maxn];
    14 
    15 int n,tot;
    16 int dp[maxn],num[maxn],head[maxn];
    17 
    18 void Inite(){
    19     tot=0;
    20     memset(head,-1,sizeof(head));
    21 } 
    22 
    23 void addedge(int u,int v){
    24     e[tot].to=v;
    25     e[tot].next=head[u];
    26     head[u]=tot++;
    27 }
    28 
    29 void DFS(int pa,int u){
    30     num[u]=1,dp[u]=0;
    31     for(int i=head[u];i!=-1;i=e[i].next){
    32         int v=e[i].to;
    33         if(pa==v) continue;
    34         DFS(u,v);
    35         dp[u]=max(dp[u],num[v]);
    36         num[u]+=num[v];
    37     } 
    38     dp[u]=max(dp[u],n-num[u]);
    39 }
    40 
    41 void Solve(){
    42     bool flag=false;
    43     for(int i=1;i<=n;i++){
    44         if(dp[i]<=n/2){
    45             flag=true;
    46             printf("%d
    ",i);
    47         }
    48     }
    49     if(!flag) printf("NONE
    ");
    50 }
    51 
    52 int main()
    53 {   Inite();
    54     scanf("%d",&n);
    55     for(int i=2;i<=n;i++){
    56         int u,v;
    57         scanf("%d%d",&u,&v);
    58         addedge(u,v);
    59         addedge(v,u);
    60     }
    61     DFS(0,1);
    62     Solve();
    63     
    64     return 0;
    65 }
  • 相关阅读:
    处处留心皆学问,世事如棋局局新…
    【转载】2017 软件测试行业现状调查报告_From_51testing_On_20180625
    Hello World In Go ...
    C# 易错题整理
    C# 函数
    C# 哈希表,队列,栈
    C# 数组,集合,泛型集合
    C# 如何生成验证码
    C# 年月日时间题+Timespan
    闰年的一个BUG
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7748532.html
Copyright © 2011-2022 走看看