[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 ...
随机推荐
- hadoop高可用安装和原理详解
本篇主要从hdfs的namenode和resourcemanager的高可用进行安装和原理的阐述. 一.HA安装 1.基本环境准备 1.1.1.centos7虚拟机安装,详情见VMware安装Cent ...
- Junit测试Service类方法教程
Junit测试是很方便的,本博客记录一下Junit测试一些Service接口的方法,这样可以不运行项目,在@Test注解的方法里直接测试 Maven引入jar包: <properties> ...
- 校园网打开IEEE 显示未登录
校园网访问IEEE 显示未登录,如图 解决办法 1.打开网络和共享中心 2.如图 3.把ipv6的钩去掉 4.把host文件(在C:\Windows\System32\drivers\etc)复制到桌 ...
- [LeetCode] 由 “找零钱" 所想
Ref: [Optimization] Dynamic programming[寻找子问题] Ref: [Optimization] Advanced Dynamic programming[优于re ...
- JNDI注入与反序列化学习总结
0x01.java RMI RMI(Remote Method Invocation)是专为Java环境设计的远程方法调用机制,远程服务器实现具体的Java方法并提供接口,客户端本地仅需根据接口类的定 ...
- 基于vue实现搜索高亮关键字
有一个需求是在已有列表中搜索关键词,然后在列表中展示含有相关关键字的数据项并且对关键字进行高亮显示,所以该需求需要解决的就两个问题: 1.搜索关键词过滤列表数据 2.每个列表高亮关键字 ps: 此问题 ...
- 读《深入理解Elasticsearch》点滴-查询分类
1.如何分类,略.个人不接受书中的分类方法,建议采用官网上的分类方法 2.term查询,可以模拟No-SQL数据库
- How to setup Electrum testnet mode and get BTC test coins
For some reason we need to use BTC test coins, but how to set up the Bitcoin testnet wallet and get ...
- Kubernetes 系列(二):Deployment 扩容
(1)首先我们创建一个nginx的Deployment,采用官方的yaml: kubectl create -f https://kubernetes.io/docs/user-guide/nginx ...
- Shell之Test命令
目录 Shell之Test命令 参考 Test简介 Test语法 表达式之逻辑运算 表达式之字符串判断 表达式之整数判断 表达式之文件判断 Shell之Test命令