http://poj.org/problem?id=3107
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
/**
poj 1655 利用树形dp求树的重心
题目大意:求树的所有重心,按从小到大的顺序输出
解题思路:树的重心定义为:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重
心后,生成的多棵树尽可能平衡. 实际上树的重心在树的点分治中有重要的作用, 可以避免N^2的极端复杂度(从退化链的一端出发),保证
nlogn的复杂度, 利用树形dp可以很好地求树的重心.
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=50005;
struct note
{
int v,next;
} edge[maxn*2];
int head[maxn],ip;
int maxx,k,n,num[maxn],cnt[maxn];
void init()
{
memset(head,-1,sizeof(head));
ip=0;
}
void addedge(int u,int v)
{
edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
}
void dfs(int u,int pre)
{
int tmp=-0x3f3f3f3f;
num[u]=1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(v==pre)continue;
dfs(v,u);
num[u]+=num[v];
tmp=max(tmp,num[v]);
}
tmp=max(tmp,n-num[u]);///除去以u为根节点的子树部分剩下的节点数
if(tmp<maxx)
{
k=0;
cnt[k++]=u;
maxx=tmp;
}
else if(tmp==maxx)
{
cnt[k++]=u;
}
}
int main()
{
while(~scanf("%d",&n))
{
init();
for(int i=1; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
memset(num,0,sizeof(num));
maxx=0x3f3f3f3f;
dfs(1,-1);
sort(cnt,cnt+k);
for(int i=0; i<k; i++)
printf(i==k-1?"%d\n":"%d ",cnt[i]);
}
return 0;
}