POJ 3107 Godfather

Source: Internet
Author: User
Tags integer numbers

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5154 Accepted: 1793
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

Key points: First find out the center of gravity of the tree, and delete the maximum number of nodes after the center of gravity, and then count a few can be
CODE:
#include <iostream>#include<cstdio>#include<cstring>#defineREP (i, S, N) for (int i = s; i <= n; i + +)#defineRep_ (i, S, N) for (int i = n; i >= s; i-)#defineMax_n 50000 + 10using namespacestd;intN, u, v;structnode1{intV, Next;} E[max_n <<1];structnode2{intsum, CNT;} N[max_n];intHead[max_n], top =0;voidAddintUintv) {e[+ + top].v = V; E[top].next = Head[u]; Head[u] =top;}intDFS (intXintLast ) {N[x].sum=1; n[x].cnt =0;  for(inti = head[x]; I i =E[i].next) {        if(E[I].V! =Last ) {            intt =DFS (E[I].V, x); N[x].sum+ = t; n[x].cnt =Max (n[x].cnt, T); }    }    returnn[x].sum;}intMain () {scanf ("%d", &N); REP (i,1N1) {scanf ("%d%d", &u, &v); Add (U, v);    Add (V, u); } DFS (1,0); intAns =9999999; REP (i,1, N) {n[i].cnt= Max (n[i].cnt, N-n[i].sum); if(Ans > n[i].cnt) ans =n[i].cnt; } REP (I,1, N)if(n[i].cnt = = ans) printf ("%d", i); return 0;} 

POJ 3107 Godfather

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.