Poj 2599 Godfather(树的重心)
来源:互联网 发布:sql语句执行顺序limit 编辑:程序博客网 时间:2024/06/08 11:37
Godfather
Time Limit: 2000MS Memory Limit: 65536K
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 ai, bi 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
/*找树的重心们.*/#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define MAXN 50001using namespace std;int n,m,rt,f[MAXN],ans[MAXN],tot,sum,cut,head[MAXN],size[MAXN];struct edge{int v,next;}e[MAXN*2];int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar(); return x*f;}void add(int u,int v){ e[++cut].v=v;e[cut].next=head[u];head[u]=cut;}void Clear(){ memset(size,0,sizeof size); memset(head,0,sizeof head); memset(f,0,sizeof f); cut=rt=tot=0;}void slove(int u,int fa){ size[u]=1; for(int i=head[u];i;i=e[i].next) { if(e[i].v==fa) continue; slove(e[i].v,u); size[u]+=size[e[i].v]; f[u]=max(f[u],size[e[i].v]); } f[u]=max(f[u],sum-size[u]); if(f[rt]>f[u]) rt=u,ans[tot=1]=u; else if(f[rt]==f[u]) ans[++tot]=u;}int main(){ int t,x,y; n=read(); Clear(); for(int i=1;i<=n-1;i++) { x=read(),y=read(); add(x,y),add(y,x); } sum=n;f[0]=1e9; slove(1,rt); sort(ans+1,ans+tot+1); for(int i=1;i<=tot;i++) printf("%d ",ans[i]); printf("\n"); return 0;}
- Poj 2599 Godfather(树的重心)
- Poj 3107 Godfather (树的重心)
- POJ 3107 Godfather (树的重心)
- poj 3107 Godfather 求树的重心
- poj 3107 Godfather(树的重心)
- 【POJ 3107 】Godfather 【树的重心】
- poj 3107 Godfather(树形dp,树的重心)
- POJ 题目3107 Godfather(树的重心)
- POJ 3107 Godfather (求树的重心)
- poj 3107 Godfather(树的重心问题)
- POJ.3107 Godfather (树形DP 树的重心)
- POJ 3107 Godfather (树的重心模板题)
- POJ3107 Godfather 求树的重心
- POJ3107-树的重心&树形DP-Godfather
- poj3107 Godfather(树的重心)
- Godfather (树形dp,树的重心)
- Godfather (树形dp + 树的重心)
- POJ-3107 Godfather & POJ-2378 Tree Cutting(树的重心)
- css之border&三角形
- Spring4深入理解01----Bean配置(依赖注入和控制反转的区别)
- 信息安全问题频发的社交里,谁来保护我们的隐私?
- 一起来玩AZURE SQL(一)AZURE SQL 介绍
- 【RabbitMQ】生产者,消费者,信道,队列,交换器和绑定
- Poj 2599 Godfather(树的重心)
- 我的前端学习之路,加油!
- Ubuntu进入root权限命令——sudo和su命令详解
- Android Studio 安装
- Git常用指令整理及说明(详细)
- Rails UVA
- 位运算
- 最长上升子序列
- Ehome:智能家居嵌入式项目背景和开发流程