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;
    }
  • 相关阅读:
    2017《Java技术》预备作业 计科1501 杨柳
    Java技术预备作业02 计科1501杨柳
    H2O.ai初步使用
    Vue.Js加入bootstrap及jquery,或加入其他插件vueresource,vuex等
    初次使用git上传代码(转)
    svg绘图工具raphael.js的使用
    EF6添加mysql的edmx实体时报错:无法生成模型:“System.Data.StrongTypingException: 表“TableDetails”中列“IsPrimaryKey”的值为 DBNull
    在window下搭建Vue.Js开发环境
    SQL Server: 索引碎片产生及修复
    Windows注册表(regedit.exe)
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8072071.html
Copyright © 2011-2022 走看看