http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5374

Building Fire Stations


Time Limit: 5 Seconds      Memory Limit: 131072 KB      Special Judge


Marjar University is a beautiful and peaceful place. There are N buildings and N - 1 bidirectional roads in the campus. These buildings are connected by roads in
such a way that there is exactly one path between any two buildings. By coincidence, the length of each road is 1 unit.

To ensure the campus security, Edward, the headmaster of Marjar University, plans to setup two fire stations in two different buildings so that firefighters are able to arrive at the
scene of the fire as soon as possible whenever fires occur. That means the longest distance between a building and its nearest fire station should be as short as possible.

As a clever and diligent student in Marjar University, you are asked to write a program to complete the plan. Please find out two proper buildings to setup the fire stations.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (2 <= N <= 200000).

For the next N - 1 lines, each line contains two integers Xi and Yi. That means there is a road connecting building Xi and
building Yi (indexes are 1-based).

Output

For each test case, output three integers. The first one is the minimal longest distance between a building and its nearest fire station. The next two integers are the indexes of the
two buildings selected to build the fire stations.

If there are multiple solutions, any one will be acceptable.

Sample Input

  1. 2
  2. 4
  3. 1 2
  4. 1 3
  5. 1 4
  6. 5
  7. 1 2
  8. 2 3
  9. 3 4
  10. 4 5

Sample Output

  1. 1 1 2
  2. 1 2 4

题意:给出n个城市和n-1条道路,要选两个城市作为消防站,问建造在那两个城市可以使距离消防站最远的城市的距离最小?

分析:先求出树的直径,然后在直径中间把边切开,成为两个新的树,分别求出两个新树的重心和直径:

  1. #include"string.h"
  2. #include"stdio.h"
  3. #include"iostream"
  4. #include"algorithm"
  5. #include"queue"
  6. #include"stack"
  7. #include"vector"
  8. #include"stdlib.h"
  9. #include"map"
  10. #include"string"
  11. #include"math.h"
  12. #define M 400009
  13. #define inf 10000000
  14. #define INF 0x3f3f3f3f
  15. using namespace std;
  16. struct node
  17. {
  18. int u,v,next;
  19. }edge[M];
  20. int t,head[M];
  21. void init()
  22. {
  23. t=0;
  24. memset(head,-1,sizeof(head));
  25. }
  26. void add(int u,int v)
  27. {
  28. edge[t].u=u;
  29. edge[t].v=v;
  30. edge[t].next=head[u];
  31. head[u]=t++;
  32. }
  33. int dis[M],pre[M],n,use[M],a[M],pref[M],a1[M],a2[M],maxi;
  34. int bfs(int u,int fuck)
  35. {
  36. maxi=0;
  37. int i;
  38. queue<int>q;
  39. for(i=1;i<=n;i++)
  40. use[i]=0;
  41. dis[u]=0;
  42. use[u]=1;
  43. q.push(u);
  44. int ID=u;
  45. while(!q.empty())
  46. {
  47. u=q.front();
  48. q.pop();
  49. for(i=head[u];~i;i=edge[i].next)
  50. {
  51. if(i==(fuck^1)||i==fuck)continue;
  52. int v=edge[i].v;
  53. if(!use[v])
  54. {
  55. use[v]=1;
  56. dis[v]=dis[u]+1;
  57. pre[v]=u;
  58. pref[v]=i;
  59. if(maxi<dis[v])
  60. {
  61. maxi=dis[v];
  62. ID=v;
  63. }
  64. q.push(v);
  65. }
  66. }
  67. }
  68. return ID;
  69. }
  70. void bary()
  71. {
  72. int i;
  73. int u=bfs(1,-2);
  74. pre[u]=-1;
  75. int v=bfs(u,-2);
  76. int cnt=0;
  77. a[++cnt]=v;
  78. for(i=pre[v];i!=-1;i=pre[i])
  79. a[++cnt]=i;
  80. int I=pref[a[cnt/2]];
  81.  
  82. int u1=bfs(a[cnt/2],I);
  83. pre[u1]=-1;
  84. int v1=bfs(u1,I);
  85. int cnt1=0;
  86. a1[++cnt1]=v1;
  87. for(i=pre[v1];i!=-1;i=pre[i])
  88. a1[++cnt1]=i;
  89.  
  90. int u2=bfs(a[cnt/2+1],I);
  91. pre[u2]=-1;
  92. int v2=bfs(u2,I);
  93. int cnt2=0;
  94. a2[++cnt2]=v2;
  95. for(i=pre[v2];i!=-1;i=pre[i])
  96. a2[++cnt2]=i;
  97.  
  98. printf("%d %d %d\n",max(cnt1/2,cnt2/2),a1[(cnt1+1)/2],a2[(1+cnt2)/2]);
  99. }
  100. int main()
  101. {
  102. int T,i;
  103. cin>>T;
  104. while(T--)
  105. {
  106. scanf("%d",&n);
  107. init();
  108. for(i=1;i<n;i++)
  109. {
  110. int a,b;
  111. scanf("%d%d",&a,&b);
  112. add(a,b);
  113. add(b,a);
  114. }
  115. bary();
  116. }
  117. return 0;
  118. }

求树的直径和中心(ZOJ3820)的更多相关文章

  1. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  2. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  3. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  4. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

  5. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

  6. HDU4612+Tarjan缩点+BFS求树的直径

    tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...

  7. hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  8. 求树的直径+并查集(bfs,dfs都可以)hdu4514

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 这题主要是叫我们求出树的直径,在求树的直径之前要先判断一下有没有环 树的直径指的就是一棵树上面距 ...

  9. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

随机推荐

  1. 关于Cocos2d-x对象的定义和创建

    游戏可以包含很多个场景,每个场景又包含很多的层,每个层又包含很多的节点,这些节点,层,场景都可以看成一个一个的对象,我们把每一个彼此不同但又是同类型的对象归为一个类,为它创建一个单独的类,这个类有这些 ...

  2. 机器学习性能评估指标(精确率、召回率、ROC、AUC)

    http://blog.csdn.net/u012089317/article/details/52156514 ,y^)=1nsamples∑i=1nsamples(yi−y^i)2

  3. ffplay的音视频同步分析

    以前工作中参与了一些音视频程序的开发,不过使用的都是芯片公司的SDK,没有研究到更深入一层,比如说音视频同步是怎么回事.只好自己抽点时间出来分析开源代码了,做音视频编解码的人都知道ffmpeg,他在各 ...

  4. put ListView in a ScrollView(bug fixed)

    Android: put ListView in a ScrollView   When I need to use ListView with other controls on the same ...

  5. 基于Linux的智能家居的设计(4)

    3  开发环境的搭建 本次课题使用的开发环境比較特殊.没有一个现成的集成开发环境,需要自己一步一步的搭建开发环境,开发环境的搭建的过程十分复杂,并且假设没有这个开发环境本次课题就无法进行. 因此.在进 ...

  6. python cython 模块(2)

    cython 的主要用途是加速python 代码的执行速度,手段有很多种,最简单的一种就是将变量声明成静态类型: 比如用python 代码写的计算素数的函数,最大计算1000个: def primes ...

  7. 【Python】多线程2

    threading模块 import time import random import threading class Inclass: def __init__(self): print 'Inc ...

  8. ubuntu-15.04-desktop-i386.iso:ubuntu-15.04-desktop-i386:安装Oracle11gR2

    ubuntu 桌面版的安装不介绍. 如何安装oracle:核心步骤和关键点. ln -sf /bin/bash /bin/sh ln -sf /usr/bin/basename /bin/basena ...

  9. C++第15周(春)项目3 - OOP版电子词典(二)

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序须要的相关 ...

  10. mybatis由浅入深day01_8输出映射_8.1resultType输出类型(8.1.1输出简单类型_8.1.2输出pojo对象和pojo列表_8.1.3输出hashmap)

    8 输出映射 8.1 resultType(输出类型) 使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功. 如果查询出来的列名和pojo中的属性名全 ...