(Poj3107godfather, tree-shaped dp,next array) The center of gravity of the tree

Source: Internet
Author: User
Tags integer numbers

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

6
1 2
2 3
2 5
3 4
3 6
Sample Output

2 3
Source

Northeastern Europe 2005, Northern subregion
Topic: Given a tree with n nodes, find out all the center of gravity of the tree and output it from large to small by number.
The definition of the center of gravity of a tree is given: the center of gravity of a tree is the deletion of the node and the partitioning of the tree into several parts, which minimizes the number of nodes in these parts.
In fact, the method is very simple.
Use son[i] to indicate how many child nodes of node I have.
Blance represents the maximum number of nodes in a subtree with node I as the father.
Then the maximum number of nodes in several parts formed after deleting node i is
Max (blance,n-son[i]-1);
And then we'll have DFS again.
Notice that the vector will time out. You need to use the next array:
Code:

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace STD;Const intn=50010;Const intinf=2100000000LLstructuse{intSt,en;} b[1000001];intson[n],n,ans[n],minn,next[5000001]={0},point[100001]={0},tot;BOOLF[n];voidAddintXintY) {tot++;next[tot]=point[x];p oint[x]=tot; B[tot].st=x;b[tot].en=y;}voidDfsintx) {intI,j,u,balance=0; son[x]=0; f[x]=false; for(I=point[x];i;i=next[i]) {u=b[i].en;if(!f[u])Continue;        DFS (U); son[x]+=son[u]+1; Balance=max (balance,son[u]+1); } Balance=max (balance,n-son[x]-1);if(Balance<minn)        {minn=balance; ans[0]=1; ans[1]=x; }Else if(Balance==minn) {ans[0]+=1; ans[ans[0]]=x; }}intMain () {intI,j,x,y;scanf("%d", &n); Minn=inf;memset(F,1,sizeof(f)); for(i=1; i<n;++i) {scanf("%d%d", &x,&y);        Add (x, y);           Add (y,x); } DFS (1); Sort (ans+1, ans+ans[0]+1); for(i=1; i<=ans[0];++i)printf("%d", Ans[i]);printf("\ n");}

(Poj3107godfather, tree-shaped dp,next array) The center of gravity of the tree

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.