Godfather

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7885   Accepted: 2786

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

分析

题目的意思很明确,就是求所有树的重心(再按字典序输出)。

那么我们先介绍一下树的重心。
树的重心定义为:
树中的一个点,删掉该点,使剩下的树所构成的森林中最大的子树节点数最少。
树的重心推论:
1.设树上的一个点S,树上其余所有点到S点的距离之和最小,那么S就是重心。
2.树的重心不唯一。
那么我们依靠定义来求树的重心好了。
首先我们确定一个根,进行一遍dfs,回溯的时候可以递归统计该点不同子树所拥有的点的数量,
然后再用(总节点数)减去(1)减去(子树),就是其父亲那边的那棵树的点的数量,
取min,最后求出即可。

code

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int MAXN = ;
const int MAXM = ; struct Edge{
int to,nxt;
}e[MAXM];
int head[MAXM],tot;
int son[MAXN];
int ans[MAXN],p,Ans = 1e9,n; inline int read() {
int x = ,f = ;char ch = getchar();
for (; ch<''||ch>''; ch = getchar())
if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar())
x = x*+ch-'';
return x*f;
}
inline void init() {
memset(head,,sizeof(head));
memset(son,,sizeof(son));
tot = ;
}
inline void add_edge(int u,int v) {
e[++tot].to = v,e[tot].nxt = head[u],head[u] = tot;
}
void dfs(int u,int fa) {
int cnt = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v==fa) continue;
dfs(v,u);
son[u] += son[v]+;
cnt = max(cnt,son[v]+);
}
cnt = max(cnt,n-son[u]-);
if (cnt<Ans) {Ans = cnt,p = ,ans[++p] = u;}
else if (cnt==Ans) {ans[++p] = u;}
}
int main() { while (scanf("%d",&n)!=EOF) {
init();
for (int u,v,i=; i<n; ++i) {
u = read(),v = read();
add_edge(u,v),add_edge(v,u);
}
dfs(,);
sort(ans+,ans+p+);
for (int i=; i<=p; ++i)
printf("%d ",ans[i]);
printf("\n");
}
return ;
}

poj 3107 Godfather(树的重心)的更多相关文章

  1. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  2. POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)

    参考网址:http://blog.csdn.net/acdreamers/article/details/16905653   树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...

  3. Poj 2599 Godfather(树的重心)

    Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...

  4. POJ 3107 Godfather (树重心)

    题目链接:http://poj.org/problem?id=3107 题意: 数重心,并按从小到大输出. 思路: dfs #include <iostream> #include < ...

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

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  6. # [Poj 3107] Godfather 链式前向星+树的重心

    [Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...

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

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

  8. POJ 3107 Godfather(树的重心)

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

  9. POJ 3107 Godfather (树的重心)

    题意:求树的重心,若有多个,全部打印出来. 思路: 树的重心:在删除点v后,森林中的每棵树的节点数尽量均匀,若最大的那棵树的节点数最小,称v为树的重心. 这道题只是求树的所有重心,当且经当这棵树有对称 ...

随机推荐

  1. Kendo UI 单页面应用(二) Router 类

    Kendo UI 单页面应用(二) Router 类 Route 类负责跟踪应用的当前状态和支持在应用的不同状态之间切换.Route 通过 Url 的片段功能(#url)和流量器的浏览历史功能融合在一 ...

  2. Date-DateFormat-Calendar-Math-regex

    一.Date类(java.util) 作用:表示时间的类,精确到毫秒,以GMT 1970年1月1日0点0分0秒起算 构造方法:     Data() ---获取当前时间      Date(long ...

  3. 织梦修改文档HTML默认保存路径

    \data\config.cache.inc.php $cfg_arcdir = '/a'; 改为 $cfg_arcdir = '/';

  4. PADS 创建封装笔记

    1.在PADS logic中新建元件和CAE封装 2.在PADS layout 中建立元件的PCB封装 3.用PADS Library Converter 把以前版本的库转化为现在的版本.

  5. Linux下端口被占用如何解决???

    有时候关闭软件后,后台进程死掉,导致端口被占用.下面以JBoss端口8083被占用为例,列出详细解决过程. 解决方法: 1.查找被占用的端口 netstat -tln netstat -tln | g ...

  6. equals()方法详解

    Java语言中equals()方法的使用可以说比较的频繁,但是如果轻视equals()方法,一些意想不到的错误就会产生.哈哈,说的有点严重了~ 先谈谈equals()方法的出身.equals()方法在 ...

  7. 【UML】状态图Statechart diagram(转)

    前言         UML由动态图和静态图组成,状态图就是属于动态图中较为重要的一张图. 定义         用来描述一个特定对象的所有可能状态以及由于各种事件的发生而引起的状态之间的转移. 目的 ...

  8. HDU - 5457 Hold Your Hand (Trie + 最小割)

    Hold Your Hand Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)T ...

  9. HDU 4741 Save Labman No.004 (几何)

    题意:求空间两线的最短距离和最短线的交点 题解: 线性代数和空间几何,主要是用叉积,点积,几何. 知道两个方向向量s1,s2,求叉积可以得出他们的公共垂直向量,然后公共垂直向量gamma和两线上的点形 ...

  10. Android(java)学习笔记110:Java中操作文件的类介绍(File + IO流)

    1.File类:对硬盘上的文件和目录进行操作的类.    File类是文件和目录路径名抽象表现形式  构造函数:        1) File(String pathname)       Creat ...