题目描述 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 Description

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 Description

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

数据范围及提示 Data Size & Hint

 

之前的一些废话:是时候准备会考了。。

题解:找树重心即可。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define mem(a,b) memset(a,b,sizeof(a))
inline int read()
{
int x=,f=;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-;c=getchar();}
while(isdigit(c)){x=x*+c-'';c=getchar();}
return x*f;
}
const int maxn=,oo=;
struct Edge
{
int u,v,next;
Edge() {}
Edge(int _1,int _2,int _3):u(_1),v(_2),next(_3) {}
}e[maxn<<];
int n,ce=-,a,b,first[maxn],size[maxn],ms[maxn],ans=oo;
bool ok;
void addEdge(int a,int b)
{
e[++ce]=Edge(a,b,first[a]);first[a]=ce;
e[++ce]=Edge(b,a,first[b]);first[b]=ce;
}
void dfs(int now,int fa)
{
size[now]=;
for(int i=first[now];i!=-;i=e[i].next)
if(e[i].v!=fa)
{
dfs(e[i].v,now);
ms[now]=max(ms[now],size[e[i].v]);
size[now]+=size[e[i].v];
}
ms[now]=max(ms[now],n-size[now]);
ans=min(ans,ms[now]);
}
int main()
{
mem(first,-);
n=read();
for(int i=;i<n;i++)a=read(),b=read(),addEdge(a,b);
dfs(,);
for(int i=;i<=n;i++)if(ans==ms[i])
{
if(!ok)printf("%d",i),ok=;
else printf(" %d",i);
}
return ;
}

总结:一个树的重心最多有两个。

最新文章

  1. 使用XStream解析MXL文件用到的jar包---xpp3_min-1.1.3.4.O.jar和xstream-1.3.1.jar
  2. Windows Phone 四、控件模版
  3. jquery .attr()
  4. jax-rs中的一些参数标注简介(@PathParam,@QueryParam,@MatrixParam,@HeaderParam,@FormParam,@CookieParam)
  5. [问题2014S15] 解答
  6. 全新安装Mac OSX 开发者环境 同时使用homebrew搭建 PHP,Nginx ,MySQL,Redis,Memcache ... ... (LNMP开发环境)
  7. sqlite:多线程操作数据库“database is locked”解决方法(二)
  8. UVA - 10574 Counting Rectangles
  9. Redis源代码分析(一)--Redis结构解析
  10. eclipse plugin 导出插件包
  11. jenkins自动化工具使用教程
  12. 盒模型、position、float详解css重点汇总
  13. 三十四、Linux 进程与信号——信号特点、信号集和信号屏蔽函数
  14. vue之node.js的简单介绍
  15. OpenStack--Havana
  16. 简单rmi示例
  17. Windows2008安装WebSphere 6.1提示此安装程序不能在图形方式中运行
  18. Ubuntu 18.10安装Firefox 和 Google Chrome
  19. python基础整理2——Linux
  20. JavaScript高级 面向对象(10)--onload与jq中ready的区别

热门文章

  1. [转]numpy的getA()/getA1()/getH()/getI()函数
  2. 物联网架构成长之路(42)-直播流媒体入门(RTMP篇)
  3. 可能会搞砸你的面试:你知道一个TCP连接上能发起多少个HTTP请求吗?
  4. SpringMVC+ajax文件上传实例教程
  5. telnet 发送邮件
  6. 正式开放 | 阿里云 10 亿级镜像服务正式支持 Helm Charts,云原生交付再加速!
  7. JWT简要说明
  8. 百度编辑器UEditor,保存图片的配置问题
  9. 当MySQL数据库遇到Syn Flooding
  10. linux 网站目录权限设置