[POJ3107] Godfather - 暴力枚举(树的重心)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8728 | Accepted: 3064 |
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
#include <iostream>
#include <cstdio>
using namespace std; int n;
struct edge
{
int nxt, to;
}ed[];
int head[], cnt;
inline void add(int x, int y){ed[++cnt]=(edge){head[x], y};head[x]=cnt;}
int siz[];
int f[];
int fat[];
int mx=1e9; inline void dfs(int x, int fa)
{
siz[x] = ;
for (register int i = head[x]; i; i = ed[i].nxt)
{
int to = ed[i].to;
if (fa == to) continue;
fat[to] = x;
dfs(to, x);
siz[x] += siz[to];
}
} int main()
{
scanf("%d", &n);
for (register int i = ; i < n; i++)
{
int x, y;
scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
dfs(, );
for (register int x = ; x <= n; x ++)
{
int sum=;
for (register int i = head[x]; i; i = ed[i].nxt)
{
int to = ed[i].to;
if (to == fat[x]) sum = max(sum, siz[] - siz[x]);
else sum = max(sum, siz[to]);
}
f[x] = sum;
mx = min(mx, f[x]);
}
for (register int i = ; i <= n; i ++)
{
if (f[i] == mx) printf("%d ", i);
}
return ;
}
[POJ3107] Godfather - 暴力枚举(树的重心)的更多相关文章
- [poj3107]Godfather_树形dp_树的重心
Godfather poj-3107 题目大意:求树的重心裸题. 注释:n<=50000. 想法:我们尝试用树形dp求树的重心,关于树的重心的定义在题目中给的很明确.关于这道题,我们邻接矩阵存不 ...
- [Swust OJ 763]--校门外的树 Plus(暴力枚举)
题目链接:http://acm.swust.edu.cn/problem/0763/ Time limit(ms): 1000 Memory limit(kb): 65535 西南某科技大学的校门外有 ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)
参考网址:http://blog.csdn.net/acdreamers/article/details/16905653 树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...
- poj 3107 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7885 Accepted: 2786 Descrip ...
- Poj 2599 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...
- poj3107(树的重心,树形dp)
题目链接:https://vjudge.net/problem/POJ-3107 题意:求树的可能的重心,升序输出. 思路:因为学树形dp之前学过点分治了,而点分治的前提是求树的重心,所以这题就简单水 ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
随机推荐
- golang中Array与Slice
在golang中有数组和Slice两种数据结构,Slice是基于数组的实现,是长度动态不固定的数据结构,本质上是一个对数组字序列的引用,提供了对数组的轻量级访问.那么在go的函数中以数组或Slice为 ...
- 多线程编程学习十一(ThreadPoolExecutor 详解).
一.ThreadPoolExecutor 参数说明 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keep ...
- EasyJson 发布
JSON库很常用了,现在开源的JSON库也有很多.但是我们仍然面临下列问题 1)时不时的爆出这个Json库出现漏洞,那个json库出现漏洞.一旦出现漏洞只能升级,想切换JSON都不成. 2)一个项目中 ...
- 编写优雅代码,从挖掉恶心的if/else 开始
背景 长话短说, 作为开发人员经常需要根据条件灵活查询数据库,不管你是用rawsql 还是EFCore, 以下类似伪代码大家都可能遇到: /// <summary> /// 灵活查询 能耗 ...
- RxSwift 中的调度器
与 ReactiveCocoa 相比,Rx 的一大优势就是更丰富的并发模型.提到并发,就不得不提多线程.在 RxSwift 中,与线程对应的概念就是调度器,本文就调度器做些介绍,包括并发调度器.串行调 ...
- Unity-遇到的问题小总结
1. event trigger后面显示不了对应的方法 原因:我是直接把脚本拖拽进去的,这是没有实例化的,拖拽进去的应该是挂载这个脚本的GameObject就可以了 2.制作prefeb 将场景中的单 ...
- elastic操作-索引重命名,索引副本数修改
目前我们使用的elastic版本为2.3.5 当前版本没有直接的curl操作可以更改索引的名称,索引的副本数. 有直接更改索引副本数的api. curl -XPUT "192.168.1.1 ...
- rpm -qa|grep nfs >/dev/null 2>&1作用
在使用一些shell命令是,经常会用到rpm -qa|grep nfs >/dev/null 2>&1之类的命令,该命令干嘛用的呢? 其实这个命令就是将rpm -qa|grep n ...
- maven 打包构建相关命令
1.命令 mvn clean package 依次执行clean.resources.compile.testResources.testCompile.test.jar(打包)等7个阶段. mvn ...
- 百万it资源百度网盘链接分享
自己大量时间整理的优质资源,容量达3000多G,有需要的朋友可以微我,资源截图: 面试资料: 书籍类: 视频类: 以上只是部分资源,想要资源的亲请加微信咨询. 欢迎加微信咨询,请备注资源: 独乐乐不 ...