zoukankan      html  css  js  c++  java
  • POJ3107Godfather(求树的重心裸题)

    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 aibi 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

    题意:

    求所有树的重心,字典序输出。

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int maxn=100010;
    int Laxt[maxn],Next[maxn],To[maxn],cnt;
    int son[maxn],sz[maxn];
    int q[maxn],tot,n,root;
    void add(int u,int v)
    {
        Next[++cnt]=Laxt[u];
        Laxt[u]=cnt;
        To[cnt]=v;
    }
    void dfs(int u,int Pre)
    {
        sz[u]=1;
        for(int i=Laxt[u];i;i=Next[i]){
            int v=To[i];
            if(v!=Pre) {
                dfs(v,u);
                sz[u]+=sz[v];
                son[u]=max(sz[v],son[u]);
            }
        }
        son[u]=max(son[u],n-sz[u]);
        if(son[u]<son[root]){
            root=u;tot=0;q[++tot]=u;
        }
        else if(son[u]==son[root])  q[++tot]=u;
    }
    int main()
    {
        while(~scanf("%d",&n)){
            memset(son,0,sizeof(son));
            memset(sz,0,sizeof(sz));
            memset(Laxt,0,sizeof(Laxt));
            int u,v; tot=cnt=0;
            for(int i=1;i<n;i++){
                scanf("%d%d",&u,&v);
                add(u,v); add(v,u);
            }              
            root=0;son[root]=0x7fffffff;
            dfs(1,0); 
            sort(q+1,q+tot+1);
            printf("%d",q[1]);  
            for(int i=2;i<=tot;i++) 
              printf(" %d",q[i]); 
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    ZJOI2006 物流运输
    codevs 1403 新三国争霸
    (一) MySQL学习笔记:MySQL安装图解
    多线程同步
    SendMessage和PostMessage区别
    VS2008 MFC 配置 Gdiplus
    IE7常用的几个快捷键 你常用的是哪个
    匆匆的六年 收获了什么
    python 代码题07 sorted函数
    python 代码题06 回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()筛选出回数
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8072071.html
Copyright © 2011-2022 走看看