任重而道远

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 aibi 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<algorithm>
#include<iostream>
using namespace std;const int N = 5e4 + 10;
bool vis[N];
struct Edge {int tov, nxt;
}e[N << 1];
int n, num, minn = 1e9;
int head[N << 1], siz[N], sson[N];void add_edge (int u, int v) {num++;e[num].tov = v;e[num].nxt = head[u];head[u] = num;
}void dfs (int u, int fa) {siz[u] = 1;for (int i = head[u]; i; i = e[i].nxt) {int v = e[i].tov;if (v == fa) continue;dfs (v, u);siz[u] += siz[v];if (sson[u] < siz[v]) sson[u] = siz[v];}sson[u] = max (sson[u], n - siz[u]);minn = min (minn, sson[u]);
}int main () {cin >> n;for (int i = 1; i < n; i++) {int u, v;scanf ("%d%d", &u, &v);add_edge (u, v);add_edge (v, u);}dfs (1, -1);for (int i = 1; i <= n; i++)if (sson[i] == minn) printf ("%d ", i);return 0;
}

POJ 3107 Godfather(树形DP(找重心))相关推荐

  1. POJ 3107 Godfather (树的重心)

    题目地址:POJ 3107 还是裸的树的重心,只不过这个要求将所有的重心都输出.很简单. 代码如下: #include <iostream> #include <string.h&g ...

  2. Tree Cutting POJ - 2378(树形DP)

    题意:有n个谷仓有n-1条路连接,问最少删除哪几个点才能使得删除点后得到的连通图的加点数不大于n/2. 分析:求树的重心的变形题,poj3107的简单版,一遍dfs从叶子到根转移找出找到以每个节点为根 ...

  3. The Lost House POJ - 2057(树形dp+贪心 (双线最优子结构问题))

    思路 题意:有一只蜗牛爬上树睡着之后从树上掉下来,发现后面的"房子"却丢在了树上面, 现在这 只蜗牛要求寻找它的房子,它又得从树根开始爬起,现在要求一条路径使得其找到房子 所要爬行 ...

  4. Godfather POJ - 3107 (求树的重心)

    题目链接 题意:给定一棵树,要求按编号从小到大输出重心. 树的重心: 定义:找到一个点满足其所有的子树中最大的子树节点数最少,那么这个点就算是树的重心. 性质: 1.删除重心后所得的所有子树,节点数不 ...

  5. POJ 3107 - Godfather

    本题与POJ 1655的区别是要把所有重心的点按顺序输出出来. // poj3107 Godfather #include <cstdio> #include <cstring> ...

  6. POJ3107 Godfather 树形dp+模拟vector

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

  7. POJ 2152 Fire(树形DP)

    题意: 思路:令F[i][j]表示 的最小费用.Best[i]表示以i为根节点的子树多有节点都找到负责消防站的最小费用. 好难的题... 1 #include<algorithm> 2 # ...

  8. POJ 3107 Godfather 笔记

    公司的从属关系呈树状,n 对从属关系,每对从属关系不能确定谁的官职大.老板在公司中职务最大且只有一个,已知删除老板后的最大连通组件的大小最小.列举可能的老板.

  9. 【树形DP】树的重心详解+多组例题详解

    目录 定义: 性质: 算法分析: POJ 1655 Balancing Act(求重心) POJ 3107 Godfather P1364 医院设置(树形DP) 定义: 树的重心也叫树的质心.对于一棵 ...

最新文章

  1. axurehtml打开不用用_还有人花钱买会员看剧?赶紧用iPhone免费追剧
  2. Android OkHttp完全解析
  3. 分析mysql日志文件_MySQL日志文件与分析
  4. 外国人看来也喜欢拜年
  5. Python特殊语法:filter、map、reduce、lambda
  6. php灰度化,PHP Imagick – 将图像转换为灰度(非常糟糕的结果)
  7. python面试题百度云下载_分享三道Python经典面试题
  8. 阿里云ECS官网域名映射及Tomcat配置调整,最终使用HTTPS和域名直接访问
  9. 5g理论速度_华为5G随行WiFi Pro 有多强 我们试一下NSA网络下的5G速度
  10. gVim取消自动备份
  11. System.UriFormatException: Invalid URI 解决方法
  12. CentOS7 原生 yum 源修改为阿里 yum 源
  13. GitHub代码上传失败的解决方法
  14. IOS开发之制作静态库
  15. android 壁纸制作教程,[教程]怎样制作Android手机壁纸/桌面
  16. 连续10年霸榜第一?程序员「最常用」的编程语言是它?
  17. 【Docker】基础(二)
  18. 如何获取百度云下载直链
  19. 车窗内观春运 镜头记录旅客百态心情
  20. 2.Maven创建以及依赖、继承、聚合

热门文章

  1. VR购物Buy+是否会让线下实体店渐渐消亡?
  2. 使用DQL命令查询数据(总篇1~3)
  3. 计算梯形面积的程序html,如何利用几何画板计算梯形的面积
  4. matlab21世纪论坛,compressive sensing 压缩感知(转) 21世纪最火的研究方向
  5. office(如:Word、Excel、PPT 等)文件轻松实现在线预览
  6. MySql 查询排名前10(包含并列)Sql
  7. Visual C++ Redistributable 各版本的官方链接
  8. 刷机案例-----谷歌pixel系列机型刷写系统的一些问题解析
  9. 琴伤+园游会+迷迭香+美人鱼+上海一九四三+威廉古堡+最后的战役+她的睫毛+麦芽糖
  10. 数据库-玩转数据-Oracle的表空间及表分区