1. 程式人生 > >POJ 3107 Godfather(樹的重心)

POJ 3107 Godfather(樹的重心)

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

PS:這個題其實就是求一棵樹的重心,要是不懂樹的重心可以看看這篇部落格傳送門

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=5e4+10;
const int mod=10007;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
struct node
{
    int v,next;
} rea[maxn<<1];
int head[maxn],num[maxn],ans[maxn];
int mi,len,sl,n;
bool vis[maxn];
void inct()
{
    me(head,-1),me(vis,0);
    mi=inf,len=1,sl=0;
}
void add(int u,int v)
{
    rea[len].v=v;
    rea[len].next=head[u];
    head[u]=len++;
}
void dfs(int u)
{
    vis[u]=1,num[u]=1;
    int t_min=-1;
    for(int i=head[u]; i!=-1; i=rea[i].next)
    {
        int v=rea[i].v;
        if(!vis[v])
        {
            dfs(v);
            num[u]+=num[v];
            t_min=max(t_min,num[v]);
        }
    }
    t_min=max(t_min,n-num[u]);
    if(t_min<mi)
    {
        sl=0;
        mi=t_min;
        ans[sl++]=u;
    }
    else if(t_min==mi)
        ans[sl++]=u;
}
int main()
{
    inct();
    scanf("%d",&n);
    for(int i=1; i<n; i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y),add(y,x);
    }
    dfs(1);
    sort(ans,ans+sl);
    for(int i=0; i<sl; i++)
        printf("%d ",ans[i]);
    printf("\n");
    return 0;
}