Bnuoj 3226 godfather

Source: Internet
Author: User
Tags integer numbers
Godfathertime limit: 2000 msmemory limit: 65536 kbthis problem will be judged on PKU. Original ID: 3107
64-bit integer Io format: % LLD Java class name: Main

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 areNPersons 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 containsN-The number of persons suspected to belong to mafia (2 ≤N≤ 50 000). Let them be numbered from 1N.

The followingN−1 lines contain two integer numbers each. The pairAI,BiMeans that the gangsterAIHas communicated with the gangsterBi. 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
61 22 32 53 43 6
Sample output
2 3
Sourcenortheastern Europe 2005: Tree DP... The vector cannot be used. It times out when it is used. Mother's egg! After removing a certain point, what is left? All its sub-trees and the top part of it. How do I calculate the number of nodes in the previous section? The total number of trees minus the number of nodes in the tree with the current node as the root is the number of nodes in the previous part of the tree. The number of nodes in the subtree is calculated recursively, the maximum number of nodes in the subtree and the number of nodes in the previous part is DP [u.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define INF 0x3f3f3f3f15 using namespace std;16 const int maxn = 50010;17 struct arc{18     int to,next;19 };20 int num[maxn],n,dp[maxn];21 int head[maxn],tot,ans;22 arc g[maxn<<2];23 void add(int u,int v){24     g[tot].to = v;25     g[tot].next = head[u];26     head[u] = tot++;27 }28 void dfs(int u,int fa){29     num[u] = 1;30     dp[u] = 0;31     for(int i = head[u]; i != -1; i = g[i].next){32         if(g[i].to == fa) continue;33         dfs(g[i].to,u);34         num[u] += num[g[i].to];35         dp[u] = max(dp[u],num[g[i].to]);36     }37     dp[u] = max(dp[u],n-num[u]);38     ans = min(ans,dp[u]);39 }40 int main() {41     int i,u,v;42     bool flag;43     while(~scanf("%d",&n)){44         memset(head,-1,sizeof(head));45         tot = 0;46         for(i = 1; i < n; i++){47             scanf("%d %d",&u,&v);48             add(u,v);49             add(v,u);50         }51         ans = INF;52         dfs(1,-1);53         flag = true;54         for(i = 1; i <= n; i++){55             if(dp[i] == ans){56                 if(flag) {flag = false;printf("%d",i);}57                 else printf(" %d",i);58             }59         }60         puts("");61     }62     return 0;63 }
View code

 

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.