poj3107 Godfather

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

poj1655 Balancing Act相似
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=5e4+10;
int T,n,tot;

int aa;char cc;
int read() {
	aa=0;cc=getchar();
	while(cc<'0'||cc>'9') cc=getchar();
	while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
	return aa;
}

int fir[maxn],nxt[2*maxn],to[2*maxn],e=0;
void add(int x,int y) {
	to[++e]=y;nxt[e]=fir[x];fir[x]=e;
	to[++e]=x;nxt[e]=fir[y];fir[y]=e;
}

struct Node{
	int pos,fa,size,maxnum;
}node[maxn];

void dfs(int pos) {
	node[pos].pos=pos;
	node[pos].size=1;
	for(int y=fir[pos];y;y=nxt[y]) {
		if(to[y]==node[pos].fa) continue;
		node[to[y]].fa=pos;dfs(to[y]);
		node[pos].maxnum=max(node[pos].maxnum,node[to[y]].size);
		node[pos].size+=node[to[y]].size;
	}
}

bool cmp(const Node& a,const Node& b) {
	int x=max(a.maxnum,tot-a.size),y=max(b.maxnum,tot-b.size);
	return x!=y? x<y:a.pos<b.pos;
}

int main() {
	int x,y;
	n=read();
	for(int i=1;i<n;++i) {
		x=read();y=read();
		add(x,y);
	}
	dfs(1);tot=node[1].size;
	sort(node+1,node+n+1,cmp);
	x=max(node[1].maxnum,tot-node[1].size);y=2;
	printf("%d",node[1].pos);
	while(max(node[y].maxnum,tot-node[y].size)==x) printf(" %d",node[y].pos),y++;
	return 0;
}

  

posted @ 2017-09-05 21:41  shixinyi  阅读(173)  评论(0编辑  收藏  举报