题目描述 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 Description

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 aibi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.

输出描述 Output Description

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

数据范围及提示 Data Size & Hint

 

之前的一些废话:是时候准备会考了。。

题解:找树重心即可。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define mem(a,b) memset(a,b,sizeof(a))
inline int read()
{
int x=,f=;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-;c=getchar();}
while(isdigit(c)){x=x*+c-'';c=getchar();}
return x*f;
}
const int maxn=,oo=;
struct Edge
{
int u,v,next;
Edge() {}
Edge(int _1,int _2,int _3):u(_1),v(_2),next(_3) {}
}e[maxn<<];
int n,ce=-,a,b,first[maxn],size[maxn],ms[maxn],ans=oo;
bool ok;
void addEdge(int a,int b)
{
e[++ce]=Edge(a,b,first[a]);first[a]=ce;
e[++ce]=Edge(b,a,first[b]);first[b]=ce;
}
void dfs(int now,int fa)
{
size[now]=;
for(int i=first[now];i!=-;i=e[i].next)
if(e[i].v!=fa)
{
dfs(e[i].v,now);
ms[now]=max(ms[now],size[e[i].v]);
size[now]+=size[e[i].v];
}
ms[now]=max(ms[now],n-size[now]);
ans=min(ans,ms[now]);
}
int main()
{
mem(first,-);
n=read();
for(int i=;i<n;i++)a=read(),b=read(),addEdge(a,b);
dfs(,);
for(int i=;i<=n;i++)if(ans==ms[i])
{
if(!ok)printf("%d",i),ok=;
else printf(" %d",i);
}
return ;
}

总结:一个树的重心最多有两个。

[POJ3107]Godfather的更多相关文章

  1. poj3107 Godfather 求树的重心

    Description Last years Chicago was full of gangster fights and strange murders. The chief of the pol ...

  2. [POJ3107] Godfather - 暴力枚举(树的重心)

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8728   Accepted: 3064 Descrip ...

  3. POJ-3107 Godfather

    题目大意:给一棵无根树,找出所有满足下面的条件的点:删除它后,最大子树中的个数最少. 题目分析:两次深搜,第一次找出子树中节点的个数,第二次维护最大子树. ps:边用vector保存时会超时... 代 ...

  4. POJ-3107 Godfather 求每个节点连接的联通块数量

    dp[n][2],维护儿子的联通块数量和父亲的联通块数量. 第一遍dfs求儿子,第二遍dfs求爸爸. #include<iostream> #include<cstring> ...

  5. POJ3107 Godfather (树形DP)

    题意:求树的重心 题解:先跑一遍dfs 预处理出这种遍历方式每个节点的儿子(含自己)的数 再跑一遍 每个点的值就是他所有儿子中取一个最大值 再和它父亲这个方向比较一下 又被卡常了 vector一直tl ...

  6. POJ3107 Godfather (树的重心)

    又是一道模板题...... 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using ...

  7. 【树形dp】Godfather

    [POJ3107]Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7212   Accepted: 253 ...

  8. 树形DP水题杂记

    BZOJ1131: [POI2008]Sta 题意:给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大. 题解:记录每个点的深度,再根据根节点的深度和逐层推导出其他点的深度和. ...

  9. [poj3107/poj2378]Godfather/Tree Cutting树形dp

    题意:求树的重心(删除该点后子树最大的最小) 解题关键:想树的结构,删去某个点后只剩下它的子树和原树-此树所形成的数,然后第一次dp求每个子树的节点个数,第二次dp求解答案即可. 此题一开始一直T,后 ...

随机推荐

  1. A1039 Course List for Student (25 分)

    一.技术总结 这里由于复杂度的限制,只能够使用vector,然后进行字符串转化:考虑到string.cin.cout会超时,可以使⽤用hash(262626*10+10)将学⽣生姓名变为int型,然后 ...

  2. 在windows上搭建git服务器教程

    1.首先,需要确保windows系统上安装并配置了Java运行环境,JDK>=1.7. 2.下载Gitblit,下载地址:http://www.gitblit.com/ 3.解压缩下载的压缩包即 ...

  3. mysql派生查询必须有别名问题记录

    最近在做mysql sql兼容,原来是oracle的sql都要保证在mysql数据库运行 业务场景:原来是一个带有子查询的sql,在oracle是可以正常运行的,迁到mysql就发现报错了,报错信息如 ...

  4. 容器网络插件那么多,博云为什么基于OVS深度自研?

    背景 从2015年开始,博云开始基于Kubernetes和容器帮助客户交付应用管理平台.在开始阶段,博云选择了业界使用度非常广泛且成熟稳定的calico作为默认的网络方案并在calico方面积累了大量 ...

  5. ubuntu16.04跑通Mask R-CNN Demo

    1. 下载源码: git clone https://github.com/matterport/Mask_RCNN 2. 安装依赖项(其实就是程序的运行环境) 我是用conda新建的虚拟环境. (1 ...

  6. 一文教您如何通过 Docker 搭建反向代理 Ngnix,并配置 Https SSL 证书

    欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注附送 100G 海量学习资源哟!! 个人网站: https://www.exception.site/docker/h ...

  7. 一篇文章,带你玩转MVVM,Dapper,AutoMapper

    一.背景 由于现在做的项目都是采用WPF来进行UI设计,开发过程中都是基于MVVM来进行开发,但是项目中的MVVM并不是真正的把实体和视图进行解耦,而是将实体和视图完全融合起来,ViewModel只是 ...

  8. python算法题 python123网站单元四题目

    目录 一:二分法求平方根 二:Collatz猜想 三:算24(只考虑满足,不考虑把所有情况找出来)   下面向大家介绍几个python算法题. 一:二分法求平方根 1.题目要求为 2.输入输出格式为 ...

  9. webpack4 code splitting

    demo 代码点此,webpack4 进行 code splitting 使用 split-chunks-plugin, 开始前先做点准备工作. start 安装: npm i -D webpack ...

  10. 轻量级手绘软件openCanvas免费版,手绘板CG手绘软件

    轻量级手绘软件openCanvas免费版,手绘板CG手绘软件 手绘软件通俗一点来说就是用手来绘画的软件,应用很宽泛如建筑,服饰陈列设计.橱窗设计.家居软装设计.空间花艺设计.美术.园林.环艺.摄影.工 ...