【树形dp】Godfather

时间:2022-08-09 07:18:25
[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 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
 
题目大意:给你N个节点的无根树,一个节点是"老大"当且仅当删去他后其节点数最大的子树最小。问有那些节点是"老大"。
试题分析:Maxt[i]表示i节点的子树中size最大的
     size[i]表示以i为根的子树的大小。
     易知:size[i]=sum(size[i->son]) 
        Maxt[i]=max(size[i->son])
     那么每个节点答案就是min(Maxt[i],N-size[i])
     最后统计一下输出就好,水题……
 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,M;
int Node[MAXN*2],Root[MAXN*2],Next[MAXN*2];
int Maxt[MAXN];
int size[MAXN];
int ans,tmp;
int te[MAXN];
int cnt; void addedge(int u,int v){
++cnt;
Node[cnt]=v;
Next[cnt]=Root[u];
Root[u]=cnt;
return ;
}
void dfs(int x,int fa){
size[x]=1;
for(int i=Root[x];i;i=Next[i]){
int t=Node[i];
if(t==fa) continue;
dfs(t,x); size[x]+=size[t];
}
return ;
}
void dfs2(int x,int fa){
for(int i=Root[x];i;i=Next[i]){
int t=Node[i];
if(t==fa) continue;
dfs2(t,x); Maxt[x]=max(size[t],Maxt[x]);
}
return ;
} int main(){
N=read();
for(int i=1;i<N;i++){
int u=read(),v=read();
addedge(u,v);
addedge(v,u);
}
dfs(1,-1); dfs2(1,-1);ans=INF;
for(int i=1;i<=N;i++){
int t=max(Maxt[i],N-size[i]);
if(ans>t) ans=t;
}
for(int i=1;i<=N;i++){
int t=max(Maxt[i],N-size[i]);
if(ans==t){
te[++tmp]=i;
}
}
for(int i=1;i<=tmp;i++) printf("%d ",te[i]);
}