Godfather

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7485   Accepted: 2638

Description

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 are n persons 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 contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.

The following n ? 1 lines contain two integer numbers each. The pair aibi means that the gangster ai has communicated with the gangster bi. 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

6
1 2
2 3
2 5
3 4
3 6

Sample Output

2 3

Source

Northeastern Europe 2005, Northern Subregion

题目大意:求删掉哪个节点后 子数最大的最小 如果有多个解 按顺序输出。

题解 :树形dp

求树的重心

就是求每个点最大的子树是多少

代码

#include<iostream>
#include<cstdio>
#define maxn 50005
using namespace std;
int n,x,y,sumedge,mi=maxn;
int dad[maxn],size[maxn],head[maxn],maxson[maxn];
struct Edge{
    int x,y,nxt;
    Edge(int x=0,int y=0,int nxt=0):
        x(x),y(y),nxt(nxt){}
}edge[maxn<<1];
void add(int x,int y){
    edge[++sumedge]=Edge(x,y,head[x]);
    head[x]=sumedge;
}

void dfs(int x){
    size[x]=1;
    for(int i=head[x];i;i=edge[i].nxt){
        int v=edge[i].y;
        if(v==dad[x])continue;
        dad[v]=x;
        dfs(v);
        size[x]+=size[v];
        if(size[v]>maxson[x])maxson[x]=size[v];
    }
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<n;i++)scanf("%d%d",&x,&y),add(x,y),add(y,x);
    dfs(1);
//    for(int i=1;i<=n;i++)printf("%d ",maxson[i]);
    for(int i=1;i<=n;i++)maxson[i]=max(maxson[i],size[1]-size[i]);
    for(int i=1;i<=n;i++)mi=min(mi,maxson[i]);
    for(int i=1;i<=n;i++)if(maxson[i]==mi)printf("%d ",i);
    return 0;
}
时间: 2024-10-27 10:49:48

Godfather的相关文章

poj3107 Godfather

Description 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

【树形dp】Godfather

[POJ3107]Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7212   Accepted: 2535 Description Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and de

POJ 3107 Godfather

Godfather 时限:2000ms Description 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 i

poj3107 Godfather 求树的重心

Description 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

poj 3107 Godfather (树形dp)

Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5064   Accepted: 1769 Description 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

【poj3107】 Godfather (树的重心)

Description 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

POJ 题目3107 Godfather(树的重心)

Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4999   Accepted: 1729 Description 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

BNUOJ 3226 Godfather

Godfather Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 310764-bit integer IO format: %lld      Java class name: Main Last years Chicago was full of gangster fights and strange murders. The chief of the pol

POJ3107——Godfather

Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4853   Accepted: 1671 Description 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