Godfather
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 4999 |
|
Accepted: 1729 |
Description
Last years Chicago is 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 is n persons known to being related to mafia. The police has traced their activity for some time, and know that some of them is communicating with each other. Based on the data collected, the chief of the police suggests so 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 di Rect subordinates is represented by the children of that node. For the purpose of conspiracy the gangsters is communicate with their direct subordinates and their direct master.
Unfortunately, though the police know gangsters ' communications, they do not know who was a master in any pair of Communica Ting persons. Thus They only has an undirected tree of communications, and does not know who godfather is.
Based on the "idea" Godfather wants to having the most possible control over Mafia, the chief of the police had made a su Ggestion that godfather was such a person that after deleting it from the communications tree the size of the largest remai Ning connected component is as small as possible. Help the police-find all potential godfathers and they would 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 is numbered from 1 to N.
The following n ? 1 lines contain the integer numbers each. The pair ai, bi means that the gangster AI have communicated with the gangster bi. It's guaranteed that the gangsters ' communications form a tree.
Output
Print the numbers of all persons that is suspected to be godfather. The numbers must is printed in the increasing order, and separated by spaces.
Sample Input
61 22 32 53 43 6
Sample Output
2 3
Source
Northeastern Europe 2005, Northern Subregionac code
#include <stdio.h> #include <string.h> #include <stdlib.h> #define INF 0xfffffff#define Max (A, B) (a>b?a:b) int head[50050],vis[50050],n,k,ans[50050],cot,son[50050],size;struct s{int U, V,next;} Edge[50050*2];int cmp (const void *a,const void *b) {return * (int *) a-* (int *) b;} void Add (int u,int v) {edge[cot].u=u;edge[cot].v=v;edge[cot].next=head[u];head[u]=cot++;} void Dfs (int u) {vis[u]=1;son[u]=0;int temp=0;for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;if (!vis[v]) { DFS (v); Son[u]+=son[v]+1;temp=max (temp,son[v]+1);}} Temp=max (temp,n-son[u]-1); if (temp<size) {k=0;ans[k++]=u;size=temp;} ElseIf (temp==size) {ans[k++]=u;}} int main () {//int n;while (scanf ("%d", &n)!=eof) {int I;memset (head,-1,sizeof (head)); Cot=0;for (i=0;i<n-1;i++) { int u,v;scanf ("%d%d", &u,&v); add (u,v); add (v,u);} K=0;memset (vis,0,sizeof (Vis)), Size=inf;dfs (1) qsort (ans,k,sizeof (ans[0)), CMP);p rintf ("%d", ans[0]); for (i=1;i <k;i++) printf ("%d", Ans[i]);p rintf ("\ n", k);}}
POJ Topic 3107 Godfather (center of gravity of the tree)