• POJ


                                                                            Godfather

    Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.

    Unfortunately, the structure of Chicago mafia is rather complicated. There are n persons known to be related to mafia. The police have traced their activity for some time, and know that some of them are communicating with each other. Based on the data collected, the chief of the police suggests that the mafia hierarchy can be represented as a tree. The head of the mafia, Godfather, is the root of the tree, and if some person is represented by a node in the tree, its direct subordinates are represented by the children of that node. For the purpose of conspiracy the gangsters only communicate with their direct subordinates and their direct master.

    Unfortunately, though the police know gangsters’ communications, they do not know who is a master in any pair of communicating persons. Thus they only have an undirected tree of communications, and do not know who Godfather is.

    Based on the idea that Godfather wants to have the most possible control over mafia, the chief of the police has made a suggestion that Godfather is such a person that after deleting it from the communications tree the size of the largest remaining connected component is as small as possible. Help the police to find all potential Godfathers and they will arrest them.

    Input

    The first line of the input file contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.

    The following n − 1 lines contain two integer numbers each. The pair ai, bi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.

    Output

    Print the numbers of all persons that are suspected to be Godfather. The numbers must be printed in the increasing order, separated by spaces.

    Sample Input
    6
    1 2
    2 3
    2 5
    3 4
    3 6
    Sample Output
    2 3
    存在多个重心 将重心存在一个数组 排序输出
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<map>
     8 #include<set>
     9 #include<vector>
    10 #include<cstdlib>
    11 #include<stack>
    12 #include<string>
    13 #define eps 0.000000001
    14 typedef long long ll;
    15 typedef unsigned long long LL;
    16 using namespace std;
    17 const int INF=0x3f3f3f3f;
    18 const int N=100000+10;
    19 int ans;
    20 int n;
    21 int a[N];
    22 int head[N];
    23 int vis[N];
    24 int son[N];
    25 struct node{
    26     int to,next;
    27 }edge[N];
    28 int num;
    29 int size;
    30 void init(){
    31     size=INF;
    32     memset(vis,0,sizeof(vis));
    33     memset(head,-1,sizeof(head));
    34     num=0;
    35 }
    36 void add(int u,int v){
    37     edge[num].to=v;
    38     edge[num].next=head[u];
    39     head[u]=num++;
    40 }
    41 int t;
    42 void DFS(int v){
    43     vis[v]=1;
    44     son[v]=0;
    45     int temp=0;
    46     for(int i=head[v];i!=-1;i=edge[i].next){
    47         int u=edge[i].to;
    48         if(vis[u]==0){
    49             DFS(u);
    50             son[v]=son[v]+son[u]+1;
    51             temp=max(temp,son[u]+1);
    52         }
    53     }
    54     temp=max(temp,n-son[v]-1);
    55     if(temp<size){
    56         t=1;
    57         a[0]=v;
    58         size=temp;
    59     }
    60     else if(temp==size){
    61         a[t++]=v;
    62     }
    63 }
    64 int main(){
    65    // int t;
    66     scanf("%d",&n);
    67     init();
    68     for(int i=0;i<n-1;i++){
    69         int u,v;
    70         scanf("%d%d",&u,&v);
    71         add(u,v);
    72         add(v,u);
    73     }
    74     DFS(1);
    75     sort(a,a+t);
    76     cout<<a[0];
    77     for(int i=1;i<t;i++){
    78         cout<<" "<<a[i];
    79     }
    80     cout<<endl;
    81         //cout<<ans<<" "<<size<<endl;
    82 }
  • 相关阅读:
    前端基础之CSS
    前端基础之HTML
    Http状态码解释
    Python操作MySQL
    MySQL忘记root密码的解决办法
    关闭MySQL数据库的几种方法
    prompt更改MySQL登陆后的提示符
    JQ例子:旋转木马
    JQ属性和css部分测试
    JQ选择器逐一测试
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6624472.html
Copyright © 2020-2023  润新知