Godfather

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 3107
64-bit integer IO format: %lld      Java class name: Main

 

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

Source

 
解题:树形dp。。。vector不能用,一用就超时,mother's egg!把某一点去掉以后,剩下什么呢?它的所有子树,以及它的上部分。上部分的节点个数如何计算?树的总数减去当前节点为根的树的节点数目就是上部分的树的节点数 ,其子树的节点数递归的时候已经计算出来了,求子树的节点个数和上部分的节点个数的最大数就是dp[u]了。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,next;
};
int num[maxn],n,dp[maxn];
int head[maxn],tot,ans;
arc g[maxn<<];
void add(int u,int v){
g[tot].to = v;
g[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u,int fa){
num[u] = ;
dp[u] = ;
for(int i = head[u]; i != -; i = g[i].next){
if(g[i].to == fa) continue;
dfs(g[i].to,u);
num[u] += num[g[i].to];
dp[u] = max(dp[u],num[g[i].to]);
}
dp[u] = max(dp[u],n-num[u]);
ans = min(ans,dp[u]);
}
int main() {
int i,u,v;
bool flag;
while(~scanf("%d",&n)){
memset(head,-,sizeof(head));
tot = ;
for(i = ; i < n; i++){
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
ans = INF;
dfs(,-);
flag = true;
for(i = ; i <= n; i++){
if(dp[i] == ans){
if(flag) {flag = false;printf("%d",i);}
else printf(" %d",i);
}
}
puts("");
}
return ;
}

BNUOJ 3226 Godfather的更多相关文章

  1. BNUOJ 52325 Increasing or Decreasing 数位dp

    传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...

  2. bnuoj 24251 Counting Pair

    一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...

  3. 2015年第4本(英文第3本):Godfather教父

    2015年的第4本书,第3本英文书. 书名:Godfather ,中文书名<教父> 作者: Mario Puzo 单词数:17万 词汇量:1万 首万词不重复词数:2200(这个我不太相信) ...

  4. bnuoj 44359 快来买肉松饼

    http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms     Case Time Lim ...

  5. BZOJ 3226: [Sdoi2008]校门外的区间

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3226 题意:初始集合S为空.模拟四种集合操作:集合并.交.差.补集并. 思路:区间 ...

  6. BNUOJ 1006 Primary Arithmetic

    Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你 ...

  7. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  8. bnuoj 25659 A Famous City (单调栈)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...

  9. bnuoj 25662 A Famous Grid (构图+BFS)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662 #include <iostream> #include <stdio.h ...

随机推荐

  1. Hdu 5336 XYZ and Drops (bfs 模拟)

    题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...

  2. [ZPG TEST 105] 扑克游戏【Huffman】

    扑克游戏 (poker) 题目描述: 有一棵无穷大的满二叉树,根为star,其余所有点的权值为点到根的距离,如图: 现在你有一些扑克牌,点数从1到13,你要把这些扑克牌全部放到这个树上: 当你把点数为 ...

  3. Apple Tree POJ - 2486

    Apple Tree POJ - 2486 题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 树形dp.复杂度可能是O(玄学),不会超过$O(nk^2)$.(反正这 ...

  4. window.form增删改查

    效果展示: 查询: 可以查询姓名:民族:姓名+民族:都是空的查询全部 取值取得是姓名: 删除: 修改: 先选中查询之后修改: 添加: 代码部分: 第一张表: 第二张表:主表,民族代码加名称 natio ...

  5. 帮助新手理解equals和hashCode

    入行快要两年,偶尔想起来equals和hash还是会有些晕,索性今天就更深入的弄明白一些,不足之处也请各位大神指出批评,共同进步. 刚开始学java的时候只是记忆性的来背,如果一个类在程序中可能进行比 ...

  6. IE主页被恶意修改处理办法

    HKEY_USERS/.DEFAULT/Software/Policies/Microsoft/Internet Explorer/Control Panel 下的DWORD值“homepage”的键 ...

  7. apropos - 在 whatis 数据库中查找字符串

    总览 (SYNOPSIS) apropos keyword ... 描述 (DESCRIPTION) apropos 命令在一些特定的包含系统命令的简短描述的数据库文件里查找关键字, 然后把结果送到标 ...

  8. 05EXtensible Stylesheet Language

    1. EXtensible Stylesheet Language 1. EXtensible Stylesheet Language XSL 指扩展样式表语言(EXtensible Styleshe ...

  9. window10换系统为windows7

    第一步 第二步 第三步 下载系统:http://www.dnxtc.net 1.GHO镜像安装器和WIN7,GHO文件必须一起放在除C盘外的其他盘的根目录 2.“GHO镜像安装器“工具上右键管理员方式 ...

  10. VirtualBox中的Linux读取Windows共享目录

    1.安装VirtualBox的增强功能.菜单 -> 设备 -> 安装增强功能此时在Linux中会载入安装包,用管理员权限运行安装即可. 2.在VirtualBox设置共享目录.设置 -&g ...