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("\n");
return 0;
}

最新文章

  1. Java基础:三目运算符
  2. 动画效果 View控件的显示和隐藏效果
  3. 备忘:SSRS技巧三则
  4. Vm下 linux与windowsxp文件共享的方法
  5. Linux下redis安装与使用
  6. [模拟]ZOJ3485 Identification Number
  7. 判断一个Bitmap图像是否是.9图
  8. Delphi - 闲来无事,自己写个Timer玩玩(多线程Timer)
  9. nginx+vsftp图片下载java代码上传
  10. Layui使用心得(1)---- 数据表格
  11. 转载:MySQL看这一篇就够了
  12. 2017年4月12日16:53:54 mysql 还有没看过的命令,spring boot rabbitmq的几种应用场景,mybaties的几种句柄及其映射规则
  13. WPF 去掉Drag a column header here to group by that column
  14. noip第13课资料
  15. Go Revel - Modules(模块)
  16. 有关Mysql的mysql_store_result函数返回NULL的情况以及其他注意事项
  17. Django的rest_framework的视图之Mixin类编写视图源码解析
  18. String、StringBuffer与StringBuilder的区别-陈远波
  19. 【Spark】SparkStreaming-高可用-HA-Master
  20. zookeeper集群的安装和配置

热门文章

  1. SRID (空间引用识别号, 坐标系)【转】
  2. 【es6】promise
  3. 使用Docker发布Asp.Net Core程序到Linux
  4. python之爬取小说
  5. 用Python爬取小说《一念永恒》
  6. docker linux下配置加速器
  7. C#中的struct(结构)为值类型,struct类型全接触
  8. Linux CPU问题排查
  9. MySQL进阶15--TCL事务控制语言--建立结束事务/设置断点--默认隔离级别--脏读/幻读/不可重复读
  10. 搭建jenkins+python+selenium+robot framework环境