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;
    }
  • 相关阅读:
    整数转字符串
    SharePoint介绍性文章
    Disable Sharepoint 2007 show as System Account when system admin login
    通过IP地址获得主机名
    从文本文件读取信息
    数据库连接池问题[转]
    企业类库问题 public key 问题[经过自己测试]
    Google Analytics异步代码创建虚拟浏览量跟踪
    同一主机上WordPress博客更换域名简易八步骤(2)
    关于application/xwwwformurlencoded等字符编码的解释说明
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8072071.html
Copyright © 2011-2022 走看看