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
 
 
题目大意:给定一颗树,求树的所有重心,按顺序输出。
这是模板题,是点分治的基础。求出所有的siz(随便dfs/bfs),就能求出每个点去掉后的剩下的最大块的大小。
 program rrr(input,output);
type
etype=record
t,next:longint;
end;
var
e:array[..]of etype;
a,q,father,siz,f,ans:array[..]of longint;
v:array[..]of boolean;
n,i,j,x,y,h,t,cnt,min:longint;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;
procedure add(x,y:longint);
begin
inc(cnt);e[cnt].t:=y;e[cnt].next:=a[x];a[x]:=cnt;
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n);
cnt:=;
for i:= to n- do begin readln(x,y);add(x,y);add(y,x); end;
fillchar(v,sizeof(v),false);
h:=;t:=;q[]:=;v[]:=true;
while h<t do
begin
inc(h);
i:=a[q[h]];
while i<> do
begin
if not v[e[i].t] then
begin
v[e[i].t]:=true;father[e[i].t]:=q[h];
inc(t);q[t]:=e[i].t;
end;
i:=e[i].next;
end;
end;
fillchar(f,sizeof(f),);
for i:= to n do siz[i]:=;
min:=n;
for i:=n downto do
begin
t:=max(f[q[i]],n-siz[q[i]]);
if t=min then begin inc(j);ans[j]:=q[i]; end
else if t<min then begin j:=;ans[]:=q[i];min:=t; end;
inc(siz[father[q[i]]],siz[q[i]]);
if siz[q[i]]>f[father[q[i]]] then f[father[q[i]]]:=siz[q[i]];
end;
if f[]<min then begin j:=;ans[]:=; end
else if f[]=min then begin inc(j);ans[j]:=; end;
fillchar(v,sizeof(v),false);
for i:= to j do v[ans[i]]:=true;
for i:= to n do if v[i] then write(i,' ');
close(input);close(output);
end.

poj3107 Godfather 求树的重心的更多相关文章

  1. poj 3107 Godfather 求树的重心【树形dp】

    poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...

  2. POJ3107 Godfather (树的重心)

    又是一道模板题...... 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using ...

  3. poj3107 求树的重心(&& poj1655 同样求树的重心)

    题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...

  4. 求树的重心(POJ1655)

    题意:给出一颗n(n<=2000)个结点的树,删除其中的一个结点,会形成一棵树,或者多棵树,定义删除任意一个结点的平衡度为最大的那棵树的结点个数,问删除哪个结点后,可以让平衡度最小,即求树的重心 ...

  5. POJ 1655 Balancing Act (求树的重心)

    求树的重心,直接当模板吧.先看POJ题目就知道重心什么意思了... 重心:删除该节点后最大连通块的节点数目最小 #include<cstdio> #include<cstring&g ...

  6. POJ 1655 求树的重心

    POJ 1655 [题目链接]POJ 1655 [题目类型]求树的重心 &题意: 定义平衡数为去掉一个点其最大子树的结点个数,求给定树的最小平衡数和对应要删的点.其实就是求树的重心,找到一个点 ...

  7. 洛谷P1395 会议(CODEVS.3029.设置位置)(求树的重心)

    To 洛谷.1395 会议 To CODEVS.3029 设置位置 题目描述 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会 ...

  8. POJ 1655 Balancing Act(求树的重心--树形DP)

    题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,假设size同样就选取编号最小的. 思路:随便选一个点把无根图转化成有根图.dfs一遍就可以dp出答案 //1348K 125MS ...

  9. POJ 3107 Godfather(树的重心)

    嘟嘟嘟 题说的很明白,就是求树的重心. 我们首先dfs一遍维护每一个点的子树大小,然后再dfs一遍,对于一个点u,选择子树中size[v]最小的那个和n - size[u]比较,取最大作为删除u后的答 ...

随机推荐

  1. odoo学习之:在tree view中显示部分数据domain的使用

    只要在window.action中他添加相应东domain即可,如: <!-- 树型列表 --> <record model="ir.actions.act_window& ...

  2. NB-IOT模组指令AT+NMSTATUS和AT+CGPADDR对比

    1. AT+NMSTATUS,这个指令是用来查询模块在IOT平台的注册情况.注册指的是lwm2m协议里面的注册机制,详细可以参考lwm2m协议. 2. AT+MREGSWT,设置重启之后,自动启动注册 ...

  3. 【转】ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.165' (113)

    原文转自:http://blog.csdn.net/chengyuqiang/article/details/54285857 1.程序报错: com.mysql.jdbc.exceptions.jd ...

  4. nuget必备插件(待续)

    DevLib.ExtensionMethods Extend Z.ExtensionMethods

  5. vbox虚拟机扩容(CentOS 7.2)

    Preface   My virtual machine was simply created by vagrant in default mode without anything about th ...

  6. 在腾讯云上安装mysql遇到的问题

    卸载mysql: 1.sudo apt-get autoremove --purge mysql-server-5.5 5.5 是数据库版本, mysql -v 显示版本信息 2.sudo apt-g ...

  7. 11-Dockerfile构建镜像

    用 Dockerfile 创建上节的 ubuntu-with-vi,其内容则为: FROM ubuntu RUN apt-get update && apt-get install v ...

  8. 面向对象编程(OOP)思想小结

    Concepts 类(class):对我们要解决问题的抽象,比如建造房子的蓝图:但实现机制上来讲,类是根据蓝图构建而成的,存储在内存中的,用来表示对象的数据. 对象(object):根据类构建的实体, ...

  9. 启动tomcat时 一闪而过解决方法(2)

    下面我先跟大家确认一下问题出现的前提条件(本机版本java:1.6.20,tomcat:6.0.32) 1)在eclipse里面启动tomcat时都是正常的. 2)在系统中配置了各种环境变量如下: J ...

  10. 大前端全栈CSS3移动端开发

    作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 本节课学习视频来源:https://ww ...