题目链接:http://poj.org/problem?id=3107

题意:

数重心,并按从小到大输出。

思路:

dfs

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. using namespace std;
  6. const int N = 5e4 + ;
  7. struct Edge {
  8. int next, to;
  9. }edge[N << ];
  10. vector <int> G[N];
  11. int dp[N], n, head[N], cnt;
  12.  
  13. inline void add(int u, int v) {
  14. edge[cnt].next = head[u];
  15. edge[cnt].to = v;
  16. head[u] = cnt++;
  17. }
  18.  
  19. int dfs(int u, int p) {
  20. dp[u] = ;
  21. int sum = ;
  22. for(int i = head[u]; ~i; i = edge[i].next) {
  23. int v = edge[i].to;
  24. if(v == p)
  25. continue;
  26. int temp = dfs(v, u);
  27. dp[u] = max(dp[u], temp);
  28. sum += temp;
  29. }
  30. dp[u] = max(dp[u], n - sum - );
  31. return sum + ;
  32. }
  33.  
  34. int main()
  35. {
  36. while(~scanf("%d", &n)) {
  37. for(int i = ; i <= n; ++i) {
  38. head[i] = -;
  39. }
  40. cnt = ;
  41. for(int i = ; i < n; ++i) {
  42. int u, v;
  43. scanf("%d %d", &u, &v);
  44. add(u, v);
  45. add(v, u);
  46. }
  47. dfs(, -);
  48. int Max = n;
  49. vector <int> ans;
  50. for(int i = ; i <= n; ++i) {
  51. Max = min(Max, dp[i]);
  52. }
  53. for(int i = ; i <= n; ++i) {
  54. if(Max == dp[i]) {
  55. ans.push_back(i);
  56. }
  57. }
  58. for(int i = ; i < ans.size(); ++i) {
  59. if(i == ans.size() - ) {
  60. printf("%d\n", ans[i]);
  61. } else {
  62. printf("%d ", ans[i]);
  63. }
  64. }
  65. }
  66. return ;
  67. }

POJ 3107 Godfather (树重心)的更多相关文章

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

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

  2. poj 3107 Godfather(树的重心)

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7885   Accepted: 2786 Descrip ...

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

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

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

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

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

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

  6. POJ 1655 Balancing Act && POJ 3107 Godfather

    题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...

  7. POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)

    参考网址:http://blog.csdn.net/acdreamers/article/details/16905653   树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...

  8. POJ 3107 Godfather(树的重心)

    嘟嘟嘟 题说的很明白,就是求树的重心. 我们首先dfs一遍维护每一个点的子树大小,然后再dfs一遍,对于一个点u,选择子树中size[v]最小的那个和n - size[u]比较,取最大作为删除u后的答 ...

  9. POJ 3107 Godfather (树的重心)

    题意:求树的重心,若有多个,全部打印出来. 思路: 树的重心:在删除点v后,森林中的每棵树的节点数尽量均匀,若最大的那棵树的节点数最小,称v为树的重心. 这道题只是求树的所有重心,当且经当这棵树有对称 ...

随机推荐

  1. python解析库之 XPath

    1. XPath (XML Path Language) XML路径语言 2. XPath 常用规则: nodename    选取此节点的所有子节点 /                    从当前 ...

  2. navigationcontroller和navigationbar和navigationitem之间的区别以及不用nib实现点击屏幕关闭虚拟键盘20130911

    1.UIViewController UIView的关系. UIView是视图,UIViewController是视图控制器,两者之间是从属关系,当创建一个UIViewController的时候,一般 ...

  3. HDU:5040-Instrusive

    Instrusive Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Proble ...

  4. ACM训练联盟周赛 A. Teemo's bad day

    65536K   Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his homework ...

  5. SQL_3_表达式、条件语句与运算

    加号的两种用法: 1.在SELECT子句中使用+号以执行对数据的运算并将结果显示出来. SELECT ITEM WHOLESALE WHOLESALE+0.15 FROM PRICE; 还可以重命名新 ...

  6. 《Python全栈开发指南》第3版 Alex著(LFXC2018)

    第一章 Python基础——Python介绍&循环语句 1.1 编程语言介绍 1.2 Python介绍 1.3 Python安装 1.4 第一个Python程序 1.5 变量 1.6 程序交互 ...

  7. 爬虫Scrapy框架-1

    Scrapy 第一步:安装 linux: pip3 install scrapy windows: 1:pip3 install wheel ,安装wheel模块 2.下载twisted:http:/ ...

  8. [python 测试框架学习篇] 分享 uiautomator测试框架

    uiautomator测试框架 :https://testerhome.com/topics/4194

  9. TOJ4537: n阶行列式

    4537: n阶行列式  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 28       ...

  10. VMware RHEL6.3 开启网络连接

    确认/etc/sysconfig/network是否存在,如果不存在,service network 命令使用不了.新建: NETWORKING=yes HOSTNAME=RHEL6. GATEWAY ...