POJ 3107 Godfather (tree-shaped DP)

Source: Internet
Author: User
Tags integer numbers

Godfather
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5064 Accepted: 1769

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

Deep Search, find the child nodes of each node to make up the number of nodes of the subtree, then, Judge update the tree or enter a new mafia leaders.


#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < algorithm> #include <queue> #include <vector>using namespace std; #define LL Long Long#define N 51000# Define MEM (a,t) memset (A,t,sizeof (a)) vector<int>g[n];int ans[n];struct ege{int v,next;}             E[n*2];int head[n];int Cnt,n,n1,mmin;int Num[n];    Log I subtree contains the number of nodes void Add (int u,int v) {e[cnt].v=v;    E[cnt].next=head[u]; head[u]=cnt++;}    int dfs (int u,int fa) {int i,v,tmp=0,t1;              Num[u]=1;        The child nodes of the current branch U and the number of itself and for (I=head[u];i!=-1;i=e[i].next) {v=e[i].v;            if (V!=FA) {Num[u]+=dfs (v,u);        Tmp=max (Tmp,num[v]); }} T1=max (Tmp,n-num[u]);        After removing the point U, the maximum number of nodes of the tree if (t1<mmin) {mmin=t1;        n1=0;    Ans[n1++]=u;    } else if (t1==mmin) {ans[n1++]=u; } return num[u];}    int main () {//freopen ("In.txt", "R", stdin);    int i,j,u,v; while (~SCANF ("%d",&N)) {mem (head,-1);        cnt=0;            for (i=1;i<n;i++) {scanf ("%d%d", &u,&v);            Add (U,V);        Add (V,u);     } mem (num,0);                 Num[i] The number of sub-tree points of the record node I n1=0;         Mmin=n;        DFS (1,-1);        Sort (ANS,ANS+N1); for (i=0;i<n1;i++) printf ("%d%c", Ans[i], (i==n1-1)? '    \ n ': '); } return 0;}







POJ 3107 Godfather (tree-shaped DP)

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.