POJ3107Godfather(求树的重心裸题)
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
题意:
求所有树的重心,字典序输出。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],cnt;
int son[maxn],sz[maxn];
int q[maxn],tot,n,root;
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void dfs(int u,int Pre)
{
sz[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=Pre) {
dfs(v,u);
sz[u]+=sz[v];
son[u]=max(sz[v],son[u]);
}
}
son[u]=max(son[u],n-sz[u]);
if(son[u]<son[root]){
root=u;tot=;q[++tot]=u;
}
else if(son[u]==son[root]) q[++tot]=u;
}
int main()
{
while(~scanf("%d",&n)){
memset(son,,sizeof(son));
memset(sz,,sizeof(sz));
memset(Laxt,,sizeof(Laxt));
int u,v; tot=cnt=;
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
root=;son[root]=0x7fffffff;
dfs(,);
sort(q+,q+tot+);
printf("%d",q[]);
for(int i=;i<=tot;i++)
printf(" %d",q[i]);
printf("\n");
}
return ;
}
POJ3107Godfather(求树的重心裸题)的更多相关文章
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- UESTC 1591 An easy problem A【线段树点更新裸题】
An easy problem A Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others ...
- poj 1655 Balancing Act 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- poj3107 求树的重心(&& poj1655 同样求树的重心)
题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...
- 求树的重心(POJ1655)
题意:给出一颗n(n<=2000)个结点的树,删除其中的一个结点,会形成一棵树,或者多棵树,定义删除任意一个结点的平衡度为最大的那棵树的结点个数,问删除哪个结点后,可以让平衡度最小,即求树的重心 ...
- POJ 1655 Balancing Act (求树的重心)
求树的重心,直接当模板吧.先看POJ题目就知道重心什么意思了... 重心:删除该节点后最大连通块的节点数目最小 #include<cstdio> #include<cstring&g ...
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- hdu_3966_Aragorn's Story(树链剖分裸题)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给你一棵树,然后给定点之间的路径权值修改,最后单点查询 题解:树链剖分裸题,这里我用树状数 ...
随机推荐
- link标签的rel属性
<link>标签定义了当前文档与 Web 集合中其他文档的关系.link 元素是一个空元素,它仅包含属性.此元素只能存在于 head 部分,不过它可出现任何次数.在 HTML 中,< ...
- 【转载】viewState详解
作者:Infinities Loop 概述 ViewState是一个被误解很深的动物了.我希望通过此文章来澄清人们对 ViewState的一些错误认识.为了达到这个目的,我决定从头到尾详细的描述一下整 ...
- curl库pycurl实例及参数详解
pycurl是功能强大的python的url库,是用c语言写的,速度很快,比urllib和httplib都快. 今天我们来看一下pycurl的用法及参数详解 常用方法: pycurl.Curl() # ...
- SQLAlchemy使用笔记--SQLAlchemy ORM(三)
參考: http://docs.sqlalchemy.org/en/rel_1_0/orm/tutorial.html#eager-loading 预先载入 前面的代码,由于是lazy load.当我 ...
- SOCKIT 在make时出现(target pattern contains no % stop)???
Make错误(***target pattern contains no % stop) 1. 问题描述 在按照SoC_SW_Lab_13.0.pdf操作时候出现了下列图片的错误 2. Bsp ...
- 允许局域网内其他主机访问本地MySql数据库
mysql的root账户,我在连接时通常用的是localhost或127.0.0.1,公司的测试服务器上的mysql也是localhost所以我想访问无法访问,测试暂停. 解决方法如下: 1,修改表, ...
- [转]浅谈Flash Socket通信安全沙箱
用过Flash socket的同学都知道,Flash socket通讯有安全沙箱问题.就是在Flash Player发起socket通信时,会向服务端获取安全策略,如果得不到服务端响应,flash将无 ...
- 知识复习(LDT+TSS+GATE+INTERRUPT)
[1]README 1.0)由于实现进程的切换任务,其功能涉及到 LDT + TSS +GATE + INTERRUPT:下面我们对这些内容进行复习: 1.1) source code from or ...
- jvmtop 监控
1 jar包 <!-- -JVMTOP监控- --> <dependency> <groupId>joda-time</groupId> <art ...
- python opener代理
链接:http://www.jb51.net/article/46495.htm https://www.cnblogs.com/cunyusup/p/7341829.html