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 ;
}
BNUOJ 3226 Godfather的更多相关文章
- BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...
- bnuoj 24251 Counting Pair
一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...
- 2015年第4本(英文第3本):Godfather教父
2015年的第4本书,第3本英文书. 书名:Godfather ,中文书名<教父> 作者: Mario Puzo 单词数:17万 词汇量:1万 首万词不重复词数:2200(这个我不太相信) ...
- bnuoj 44359 快来买肉松饼
http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms Case Time Lim ...
- BZOJ 3226: [Sdoi2008]校门外的区间
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3226 题意:初始集合S为空.模拟四种集合操作:集合并.交.差.补集并. 思路:区间 ...
- BNUOJ 1006 Primary Arithmetic
Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你 ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
- bnuoj 25659 A Famous City (单调栈)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...
- bnuoj 25662 A Famous Grid (构图+BFS)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662 #include <iostream> #include <stdio.h ...
随机推荐
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- Android上的线程安全
Thread-safe methods In some situations, the methods you implement might be called from more than one ...
- (五)SpringIoc之Bean的作用域
此文转自 https://blog.csdn.net/icarus_wang/article/details/51586776# 有状态bean和无状态bean请看 https://blog.csdn ...
- HTML5的音频播放和视频播放
1.音频播放 audio(音频) html5提供了播放音频文件的标准 <audio src="anli.mp3" controls="controls" ...
- iOS---UICollectionView自定义流布局实现瀑布流效果
自定义布局,实现瀑布流效果 自定义流水布局,继承UICollectionViewLayout 实现一下方法 // 每次布局之前的准备 - (void)prepareLayout; // 返回所有的尺寸 ...
- shell脚本的练习
创建一个以.sh结束的文件. 规则: 文件的头部使用#!/bin/sh 开头 这个是一个标识的作用,告诉使用哪种脚本来执行 echo 用来向命令行来输出的东西
- windows测试物理网络
ping 192.168.10.88 -t ,参数-t是等待用户去中断测试
- 小b和灯泡
2489 小b和灯泡 2 秒 262,144 KB 10 分 2 级题 小b有n个关闭的灯泡,编号为1...n. 小b会进行n轮操作,第i轮她会将编号为i的倍数的灯泡的开关状态取反,即开变成关,关 ...
- 小b重排字符串
2485 小b重排字符串 2 秒 262,144 KB 5 分 1 级题 小b有一个字符串S,现在她希望重排列S,使得S中相邻字符不同. 请你判断小b是否可能成功. 样例解释:将"aab ...
- Hibernate修改操作 删除操作 查询操作 增加操作 增删改查 Hibernate增删查改语句
我用的数据库是MySQL,实体类叫User public class User { private Integer uid; private String username; private Stri ...