[POJ3107]Godfather
题目描述 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 ai, bi 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的更多相关文章
- poj3107 Godfather 求树的重心
Description Last years Chicago was full of gangster fights and strange murders. The chief of the pol ...
- [POJ3107] Godfather - 暴力枚举(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8728 Accepted: 3064 Descrip ...
- POJ-3107 Godfather
题目大意:给一棵无根树,找出所有满足下面的条件的点:删除它后,最大子树中的个数最少. 题目分析:两次深搜,第一次找出子树中节点的个数,第二次维护最大子树. ps:边用vector保存时会超时... 代 ...
- POJ-3107 Godfather 求每个节点连接的联通块数量
dp[n][2],维护儿子的联通块数量和父亲的联通块数量. 第一遍dfs求儿子,第二遍dfs求爸爸. #include<iostream> #include<cstring> ...
- POJ3107 Godfather (树形DP)
题意:求树的重心 题解:先跑一遍dfs 预处理出这种遍历方式每个节点的儿子(含自己)的数 再跑一遍 每个点的值就是他所有儿子中取一个最大值 再和它父亲这个方向比较一下 又被卡常了 vector一直tl ...
- POJ3107 Godfather (树的重心)
又是一道模板题...... 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using ...
- 【树形dp】Godfather
[POJ3107]Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7212 Accepted: 253 ...
- 树形DP水题杂记
BZOJ1131: [POI2008]Sta 题意:给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大. 题解:记录每个点的深度,再根据根节点的深度和逐层推导出其他点的深度和. ...
- [poj3107/poj2378]Godfather/Tree Cutting树形dp
题意:求树的重心(删除该点后子树最大的最小) 解题关键:想树的结构,删去某个点后只剩下它的子树和原树-此树所形成的数,然后第一次dp求每个子树的节点个数,第二次dp求解答案即可. 此题一开始一直T,后 ...
随机推荐
- ORB-SLAM2初步(源码逻辑分析)
今天主要是梳理一下ORB-SLAM2源码的逻辑关系,GitHub和泡泡机器人上有很好的注释版本(吴博),大神请(轻)板砖. 一.文件 如图所示,Examples里面存放的分别是基于单目.双目.RGBD ...
- SuperSocket 2.0 发布第一个预览版, 另寻找Yang Fan哥哥
昨天,SuperSocket的作者发布了2.0版本的第一个预览版.SuperSocket 2.0 是一个经过全新设计的,第一个完全基于.NET Core的版本.作者正在积极尝试提供更简单易用的API的 ...
- linux系统中启动daytime服务
环境:vmware下面的cetos6 1. 进入/etc/xinetd.d/目录下,查看是否存在daytime服务,若不存在,则须安装: 执行命令:yum install xinetd 该命令执行后会 ...
- HBase的java操作,最新API。(查询指定行、列、插入数据等)
关于HBase环境搭建和HBase的原理架构,请见笔者相关博客. 1.HBase对java有着较优秀的支持,本文将介绍如何使用java操作Hbase. 首先是pom依赖: <dependency ...
- Python 文件readlines()方法
原文连接:https://www.runoob.com/python/file-readlines.html readlines()方法用于读取所有行(直到结束符EOF)并返回列表,该列表可以由pyt ...
- go中interface空指针不为nil判断方法
interface空指针不为nil 当把一个空指针对象赋值给一个interface后,再判断!= nil就不再成立了 代码如下 package main import "fmt" ...
- 【机器学习笔记】来吧!解析k-NN
序: 监督型学习与无监督学习,其最主要区别在于:已知的数据里面有没有标签(作为区别数据的内容). 监督学习大概是这个套路: 1.给定很多很多数据(假设2000个图片),并且给每个数据加上标签(与图片一 ...
- ADO.NET中的5个主要对象
1.Connection:主要是开启程序和数据库之间的连接.没有利用连接对象将数据库打开,是无法从数据库中取得数据的. Close和Dispose的区别,Close以后还可以Open,Dispose以 ...
- 如何通过调优攻破 MySQL 数据库性能瓶颈?
一.前言 MySQL调优对于很多程序员而言,都是一个非常棘手的问题,多数情况都是因为对数据库出现问题的情况和处理思路不清晰.在进行MySQL的优化之前必须要了解的就是MySQL的查询过程,很多的查询优 ...
- Java生鲜电商平台-服务器部署设计与架构
Java生鲜电商平台-服务器部署设计与架构 补充说明:Java开源生鲜电商平台-服务器部署设计与架构,指的是通过服务器正式上线整个项目,进行正式的运营. 回顾整个章节,我们涉及到以下几个方面: 1. ...