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 ;
}

最新文章

  1. css-css权威指南学习笔记7
  2. Thread比Task多出的无法代替的部分
  3. delphi 图像旋转
  4. hdu 5676 ztr loves lucky numbers
  5. Web前端新人笔记之CSS结构和层叠
  6. 获取服务器端ip
  7. bzoj 2648 SJY摆棋子 kd树
  8. codevs 1028 花店橱窗布置 KM算法
  9. android中像素单位dp、px、pt、sp的比较
  10. chrome手动添加拓展
  11. 面试 Linux 运维工作至少需要知道哪些知识?
  12. Golang常见误区(一)
  13. notepad++上直接运行python文件
  14. springMVC 接收json字符串参数
  15. 再谈kbmMW垃圾回收
  16. Java类(继承)初始化顺序
  17. SqlServer 获取汉字的拼音首字母
  18. scala函数进阶篇
  19. centos下安装升级python到python3.5
  20. JSON传参

热门文章

  1. _bzoj2818 Gcd【线性筛法 欧拉函数】
  2. Java中的流(2)字节流-InputStream和OutputStream
  3. C# winform与Javascript的相互调用[转]
  4. Lena Sort 构造题,很多细节的模拟
  5. Cenos7 切换单用户模式
  6. C#连接数据库_使用读取配置文件的方式
  7. 初学Ajax
  8. 原创 SqlParameter 事务 批量数据插入
  9. A/B宣言
  10. 快速开发框架天梭(Tissot)