zoukankan      html  css  js  c++  java
  • poj 3107 Godfather

    Godfather
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 7485   Accepted: 2638

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

    Source

    Northeastern Europe 2005, Northern Subregion
     
    题目大意:求删掉哪个节点后 子数最大的最小 如果有多个解 按顺序输出。
     
    题解 :树形dp
    求树的重心
    就是求每个点最大的子树是多少
     
    代码
    #include<iostream>
    #include<cstdio>
    #define maxn 50005
    using namespace std;
    int n,x,y,sumedge,mi=maxn;
    int dad[maxn],size[maxn],head[maxn],maxson[maxn];
    struct Edge{
        int x,y,nxt;
        Edge(int x=0,int y=0,int nxt=0):
            x(x),y(y),nxt(nxt){}
    }edge[maxn<<1];
    void add(int x,int y){
        edge[++sumedge]=Edge(x,y,head[x]);
        head[x]=sumedge;
    }
    
    void dfs(int x){
        size[x]=1;
        for(int i=head[x];i;i=edge[i].nxt){
            int v=edge[i].y;
            if(v==dad[x])continue;
            dad[v]=x;
            dfs(v);
            size[x]+=size[v];
            if(size[v]>maxson[x])maxson[x]=size[v];
        }
    }
    
    int main(){
        scanf("%d",&n);
        for(int i=1;i<n;i++)scanf("%d%d",&x,&y),add(x,y),add(y,x);
        dfs(1);
    //    for(int i=1;i<=n;i++)printf("%d ",maxson[i]);
        for(int i=1;i<=n;i++)maxson[i]=max(maxson[i],size[1]-size[i]);
        for(int i=1;i<=n;i++)mi=min(mi,maxson[i]);
        for(int i=1;i<=n;i++)if(maxson[i]==mi)printf("%d ",i);
        return 0;
    }
  • 相关阅读:
    【转载】著名黑客雷蒙评价几种编程语言
    【GUI开发】Swing的一本极好的入门教材
    【数据结构】数组操作(HighArrayApp.java)
    数据另存为CSV档案(也是一种excel档案)【2】------自主选择路径
    TIniFile实现打开窗体后还原用户之前的配置的功能
    sql server内连接(inner join)、外连接(left outer join、right outer join、full outer join)、记录合并(union、union all)
    @指针、Cardinal()、Integer()、指针取值解析
    关于php编程的一些小技巧
    数据库操作,同时更新多条数据
    省份,城市,地区------三级联动菜单//要加注释
  • 原文地址:https://www.cnblogs.com/zzyh/p/7479209.html
Copyright © 2011-2022 走看看