BNUOJ 3226 Godfather
Godfather
This 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 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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
};
int num[maxn],n,dp[maxn];
int head[maxn],tot,ans;
arc g[maxn<<];
void add(int u,int v){
g[tot].to = v;
g[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u,int fa){
num[u] = ;
dp[u] = ;
for(int i = head[u]; i != -; i = g[i].next){
if(g[i].to == fa) continue;
dfs(g[i].to,u);
num[u] += num[g[i].to];
dp[u] = max(dp[u],num[g[i].to]);
}
dp[u] = max(dp[u],n-num[u]);
ans = min(ans,dp[u]);
}
int main() {
int i,u,v;
bool flag;
while(~scanf("%d",&n)){
memset(head,-,sizeof(head));
tot = ;
for(i = ; i < n; i++){
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
ans = INF;
dfs(,-);
flag = true;
for(i = ; i <= n; i++){
if(dp[i] == ans){
if(flag) {flag = false;printf("%d",i);}
else printf(" %d",i);
}
}
puts("");
}
return ;
}
最新文章
- css-css权威指南学习笔记7
- Thread比Task多出的无法代替的部分
- delphi 图像旋转
- hdu 5676 ztr loves lucky numbers
- Web前端新人笔记之CSS结构和层叠
- 获取服务器端ip
- bzoj 2648 SJY摆棋子 kd树
- codevs 1028 花店橱窗布置 KM算法
- android中像素单位dp、px、pt、sp的比较
- chrome手动添加拓展
- 面试 Linux 运维工作至少需要知道哪些知识?
- Golang常见误区(一)
- notepad++上直接运行python文件
- springMVC 接收json字符串参数
- 再谈kbmMW垃圾回收
- Java类(继承)初始化顺序
- SqlServer 获取汉字的拼音首字母
- scala函数进阶篇
- centos下安装升级python到python3.5
- JSON传参