zoukankan      html  css  js  c++  java
  • Godfather POJ

    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

    树的重心:对于一棵n个结点的无根树,找到一个点,使得把树变成以该点为根的有根树时,最大子树的结点数最小。换句话说,删除这个点后最大连通快(一定是树)的结点数最小。
     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=5e4+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     
    43     int temp=dp[1];
    44     for(int i=1;i<=n;i++) if(temp>dp[i]) temp=dp[i];
    45     
    46     vector<int> ans;
    47     for(int i=1;i<=n;i++) if(dp[i]==temp) ans.push_back(i);
    48     
    49     printf("%d",ans[0]);
    50     for(int i=1;i<ans.size();i++) printf(" %d",ans[i]);
    51     printf("
    ");
    52 }
    53 
    54 int main()
    55 {   Inite();
    56     scanf("%d",&n);
    57     for(int i=2;i<=n;i++){
    58         int u,v;
    59         scanf("%d%d",&u,&v);
    60         addedge(u,v);
    61         addedge(v,u);
    62     }
    63     DFS(0,1);
    64     Solve();
    65     
    66     return 0;
    67 }
  • 相关阅读:
    JavaScript学习之路-语法
    JavaScript学习之路-语法
    JavaScript学习之路-为什么要学习JavaScript语法
    JavaScript学习之路-为什么要学习JavaScript语法
    net4:Panel动态添加控件及隐藏,Table动态创建表格
    net1:DateTime,Application与Session,
    C#精髓第四讲 GridView 72般绝技
    J2ME开发基本语法及小实例专题
    net6:创建Membership对象数据源的代码
    net4:GridView中的重要操作(添加checkbox,以及鼠标动作,行颜色等)
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7748507.html
Copyright © 2011-2022 走看看