zoukankan      html  css  js  c++  java
  • Poj 2599 Godfather(树的重心)

    Godfather
    Time Limit: 2000MS Memory Limit: 65536K
    Description
    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
    Source
    Northeastern Europe 2005, Northern Subregion

    /*
    找树的重心们.
    */
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #define MAXN 50001
    using namespace std;
    int n,m,rt,f[MAXN],ans[MAXN],tot,sum,cut,head[MAXN],size[MAXN];
    struct edge{int v,next;}e[MAXN*2];
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
        return x*f;
    }
    void add(int u,int v)
    {
        e[++cut].v=v;e[cut].next=head[u];head[u]=cut;
    }
    void Clear()
    {
        memset(size,0,sizeof size);
        memset(head,0,sizeof head);
        memset(f,0,sizeof f);
        cut=rt=tot=0;
    }
    void slove(int u,int fa)
    {
        size[u]=1;
        for(int i=head[u];i;i=e[i].next)
        {
            if(e[i].v==fa) continue;
            slove(e[i].v,u);
            size[u]+=size[e[i].v];
            f[u]=max(f[u],size[e[i].v]);
        }
        f[u]=max(f[u],sum-size[u]);
        if(f[rt]>f[u]) rt=u,ans[tot=1]=u;
        else if(f[rt]==f[u]) ans[++tot]=u;
    }
    int main()
    {
        int t,x,y;
        n=read();
        Clear();
        for(int i=1;i<=n-1;i++)
        {
            x=read(),y=read();
            add(x,y),add(y,x);
        }
        sum=n;f[0]=1e9;
        slove(1,rt);
        sort(ans+1,ans+tot+1);
        for(int i=1;i<=tot;i++) printf("%d ",ans[i]);
        printf("
    ");
        return 0;
    }
  • 相关阅读:
    python+matplotlib制作雷达图3例分析和pandas读取csv操作
    python+pygame的导弹追踪鼠标游戏设置和说明
    python+pygame制作一个可自定义的动态时钟和详解
    python+tkinter制作一个可自定义的动态时钟及详细解释,珍藏版
    教你如何用python和pygame制作一个简单的贪食蛇游戏,可自定义
    博客园的博客之美化日历
    博客园的博客美化之-增加首页音乐播放器APlayer
    博客园的博客美化之文章推荐和反对按钮、看板娘、图片放大、github链接、返回顶部小火箭
    设计模式之装饰器模式
    设计模式之状态模式
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068039.html
Copyright © 2011-2022 走看看