Godfather
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7536   Accepted: 2659

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

  1. 6
  2. 1 2
  3. 2 3
  4. 2 5
  5. 3 4
  6. 3 6

Sample Output

  1. 2 3

Source

Northeastern Europe 2005, Northern Subregion
 
题意:输出一棵树的所有重心
思路:树形dp。STL超时,用前向星存图。
代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<queue>
  6. #include<stack>
  7. #include<map>
  8. #include<vector>
  9. #include<set>
  10. #include<bitset>
  11. using namespace std;
  12. #define PI acos(-1.0)
  13. #define eps 1e-8
  14. typedef long long ll;
  15. typedef pair<int,int> P;
  16. const int N=1e5+,M=1e5+;
  17. const int inf=0x3f3f3f3f;
  18. const ll INF=1e18+,mod=1e9+;
  19. int n;
  20. vector<int>G[N];
  21. int ans;
  22. int si[N],maxx[N];
  23. struct edge
  24. {
  25. int from,to;
  26. int next;
  27. };
  28. edge es[M];
  29. int cnt,head[N];
  30. void init()
  31. {
  32. cnt=;
  33. memset(head,-,sizeof(head));
  34. }
  35. void addedge(int u,int v)
  36. {
  37. cnt++;
  38. es[cnt].from=u,es[cnt].to=v;
  39. es[cnt].next=head[u];
  40. head[u]=cnt;
  41. }
  42. int dfs(int u,int fa)
  43. {
  44. for(int i=head[u]; i!=-; i=es[i].next)
  45. {
  46. int v=es[i].to;
  47. if(v==fa) continue;
  48. si[u]+=dfs(v,u);
  49. maxx[u]=max(maxx[u],si[v]);
  50. }
  51. si[u]++;
  52. maxx[u]=max(maxx[u],n-si[u]);
  53. ans=min(ans,maxx[u]);
  54. return si[u];
  55. }
  56. int main()
  57. {
  58. while(scanf("%d",&n)!=EOF)
  59. {
  60. init();
  61. for(int i=; i<n; i++)
  62. {
  63. int u,v;
  64. scanf("%d%d",&u,&v);
  65. addedge(u,v);
  66. addedge(v,u);
  67. }
  68. memset(si,,sizeof(si));
  69. memset(maxx,,sizeof(maxx));
  70. ans=inf;
  71. dfs(,);
  72. int cou=;
  73. for(int i=; i<=n; i++)
  74. {
  75. if(maxx[i]==ans&&!cou) printf("%d",i),cou++;
  76. else if(maxx[i]==ans) printf(" %d",i),cou++;
  77. }
  78. printf("\n");
  79. for(int i=; i<=n+; i++) G[i].clear();
  80. }
  81. return ;
  82. }

前向星存图 树形dp

POJ 3107.Godfather 树形dp的更多相关文章

  1. poj 3107 Godfather 求树的重心【树形dp】

    poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...

  2. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  3. # [Poj 3107] Godfather 链式前向星+树的重心

    [Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...

  4. [POJ 1155] TELE (树形dp)

    题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...

  5. Apple Tree POJ - 2486 (树形dp)

    题目链接: D - 树形dp  POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...

  6. Anniversary party POJ - 2342 (树形DP)

    题目链接:  POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...

  7. POJ 3342 (树形DP)

    题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...

  8. POJ 2342 (树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 ...

  9. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

随机推荐

  1. 围绕Buganizer的产品流程

    做技术的一定知道缺陷跟踪系统(bug系统),更不用说做测试的了,不过普遍都认为这系统是用来记录bug的,其实在google内部,这套系统是产品/项目围绕的核心.Google Buganizer扩展了类 ...

  2. android查看屏幕密度,分辨率的shell命令

    adb shell dumpsys window displays

  3. JavaScript基础应用

    1.实现字符串的反向输出 var s="abc" s.split('').reverse().join('')​  -----> "cab" 知识点: S ...

  4. HTML/CSS基础知识(一)

    Q:浏览器页面有哪三层构成,分别是什么,作用是什么? A:由三部分构成: 网页结构层(Structural Layer)——由(X)HTML等标记语言负责创建,实现页面结构. 网页表示层(Presen ...

  5. pygame 简单播放音乐程序

    环境: python2.7 pygame 功能: 播放指定目录下的歌曲(暂时mp3),可以上一曲.下一曲播放. 文件目录: font  字体文件夹 image  图片文件夹 music  音乐文件夹 ...

  6. python实战博客

    2018-10-31 更新Logging日志记录以及异常捕获 感谢廖大教程.Python实战 直接在闲置的服务器上开发.阿里云Centos 6.8 64位. 1 搭建开发环境 Python 环境是Py ...

  7. [UGUI]圆形Image

    参考链接: http://www.cnblogs.com/leoin2012/p/6425089.html 前面说过Mask组件会影响性能:https://www.cnblogs.com/lyh916 ...

  8. 推特算法,分布式ID

    package casclient_demo1.util; import java.lang.management.ManagementFactory; import java.net.InetAdd ...

  9. Python函数学习

    def func(x, y): print(x) print(y) func(x=1,2) #报错,关键参数一定不能放在位置参数前面 func(1,y=2) #可以正常运行 #参数组,适用于实参个数不 ...

  10. leetcode75

    class Solution { public: void sortColors(vector<int>& nums) { sort(nums.begin(), nums.end( ...