• 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;
    }
  • 相关阅读:
    NET Core中实现一个Token base的身份认证
    自定义一个服务器感受一下管道是如何监听、接收和响应请求的
    把商品卖给用户
    Mac版Visual Studio预览版
    Docker实战
    大数据的四大特点
    ElasticSearch Nosql
    Oracle和Elasticsearch数据同步
    关于MySql链接url参数的设置 专题
    linux date
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8072071.html
Copyright © 2020-2023  润新知