[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 ...
随机推荐
- hbase 修复 hbase hbck
hbase hbck 新版本的 hbck 可以修复各种错误,修复选项是: (1)-fix,向下兼容用,被-fixAssignments替代 (2)-fixAssignments,用于修复region ...
- 疑难杂症----Windows10
现在大多数个人电脑所用的操作系统都是win10,而我们使用win10时总是会碰上各种各样的问题,所以专门写一篇博客来记录我碰上的各种问题,便于以后更快的解决问题. 一.小娜搜索不到应用问题解决方案 小 ...
- charles 镜像工具
本文参考:charles 镜像工具 镜像工具会在你浏览指定网站时,把抓取到的文件克隆一份,并保存在你指定的路径下: 注意:如果你配置是www.aaa.com; 那么只会抓这个域名下的文件,这个域名如果 ...
- 链表实现比较高效的删除倒数第k项
最近写链表不太顺,无限的段错误.今天中午写的链表删除倒数第k项,用的带尾节点的双向链表,感觉已经把效率提到最高了,还是超时,改了很多方法都不行,最 终决定看博客,发现原来是审题错了,阳历给的是以-1结 ...
- ASP.NET Core 3.0 原生DI拓展实现IocManager
昨天.NET Core 3.0 正式发布,创建一个项目运行后发现:原来使用的Autofac在ConfigureServices返回IServiceProvider的这种写法已经不再支持.当然Autof ...
- 品Spring:bean定义上梁山
认真阅读,收获满满,向智慧又迈进一步... 技术不枯燥,先来点闲聊 先说点好事高兴一下.前段时间看新闻说,我国正式的空间站建设已在进行当中.下半年,长征五号B运载火箭将在海南文昌航天发射场择机将空间站 ...
- docker的使用---创建新的镜像(通过修改容器,个人练手理解过程记录,不推荐使用)
docker基础命令 ##列出docker客户端命令 docker docker container --help ##显示docker的版本和信息 docker --version docker v ...
- Android [启动方式:standard singleTop singleTask singleInstance]
栈顶Activity是当前正在显示的. 以A.B举例 1.standard 不同的Activity都存放在同一个栈中,每次创建实例都会堆放到栈顶,逐次返回直至退出. 创建实例B 创建实例A 点击返回时 ...
- Android测试环境配置
测试是软件开发中非常重要的一部分,Android中是使用junit测试框架,本文使用的是junit4和Android Studio.Android测试主要分两类本地测试和Instrumented测试, ...
- ubuntu13 eclipse菜单栏失效解决
使用ubuntu13安装完eclipse和myeclipse后发现菜单栏单击时不显示下拉框只能通过快捷键显示. 百度了一下,找到以下解决办法. 打开终端运行下面的命令,打开eclipse后可正常显示菜 ...