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

      给一个树,要求删掉一个点后,让剩下的部分组成的树们之中,节点最多的最少,和S题一个意思,求的是树的重心,只是输出的结果有变,让把所有可能结果输出,直接用S题改的。但是S题开始只是用的vector+数组存的数据,这个题改完之后老是超时,从这里以后才用起了邻接链表,又回头把S也补上了一个。

代码如下:

#include<stdio.h>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=50010;
struct Edge
{
    int next;
    int to;
}edge[MAX*2];
int head[MAX];
int vis[MAX];
int dp[MAX];
int num[MAX];
int N, K;
int ans, ansdex;
void addEdge(int u, int v)
{
    edge[++K].next=head[u];
    edge[K].to=v;
    head[u]=K;
}
void dfs(int now)
{
    vis[now]=1;
    num[now]=1;
    for(int i=head[now]; i!=0; i=edge[i].next)
    {
        int next=edge[i].to;
        if(vis[next])
            continue;
        dfs(next);
        dp[now]=max(dp[now], num[next]);
        num[now]+=num[next];
    }
    dp[now]=max(dp[now], N-num[now]);
    //cout<<now<<"  .....dp  "<<dp[now]<<endl;
    if(ans>dp[now])
    {
        ans=dp[now];
        ansdex=now;
    }
}
int main()
{
    //ios::sync_with_stdio(0);
    int a, b;
    while(~scanf("%d", &N))
    {
        memset(dp, 0, sizeof(dp));
        memset(vis, 0, sizeof(vis));
        memset(num, 0, sizeof(num));
        memset(edge, 0, sizeof(edge));
        K=0;
        ans=N+1;
        ansdex=0;
        for(int i=1; i<=N-1; i++)
        {
            scanf("%d%d", &a, &b);
            addEdge(a, b);
            addEdge(b, a);
            //cin>>a>>b;
            //son[a].push_back(b);
            //son[b].push_back(a);
        }
        dfs(1);
        //cout<<ansdex<<" "<<ans<<endl;
        int answer[MAX];
        int k=0;
        for(int i=1; i<=N; i++)
        {
            if(dp[i]==ans)
                answer[++k]=i;
        }
        for(int i=1; i<k; i++)
        {
            printf("%d ", answer[i]);
            //cout<<answer[i]<<" ";
        }
        printf("%d\n", answer[k]);
        //cout<<answer[k]<<endl;
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值