H. Capital City

Time Limit: 3000ms
Memory Limit: 262144KB

This problem will be judged on CodeForcesGym. Original ID: 100676H
64-bit integer IO format: %I64d      Java class name: (Any)

 

Bahosain has become the president of Byteland, he is doing his best to make people's lives easier. Now, he is working on improving road networks between the cities.
If two cities are strongly connected, people can use BFS (Bahosain's Fast Service) to travel between them in no time. Otherwise, they have to follow one of the shortest paths between them, and of course, they will use BFS when they can! Two cities are connected if there is a path between them, and they are strongly connected if after removing any single road they will remain connected. President Bahosain wants to minimize the maximum distance people have to travel from any city to reach the capital city, can you help him in choosing the capital city?

Input
The first line of input contains one integer T, the number of test cases (1 ≤ T ≤ 64).
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100,000) (0 ≤ m ≤ 200,000), the number of cities and the number of roads, respectively.
Each of the following m lines contains three space-separated integers a, b, c (1 ≤ a, b ≤ n) (1 ≤ c ≤
100,000), meaning that there is a road of length c connecting the cities a and b.
Byteland cities are connected since Bahosain became the president.
Test cases are separated with a blank line.

Output
For each test case, print the number of the city and length of the maximum shortest path on a
single line. If there is more than one possible city, print the one with the minimum number.

Sample Input

1

7 7

1 2 5

1 7 5

3 2 5

1 3 5

3 4 3

6 4 1

4 5 3

Sample Output

1 6

解题:边双连通分量 + 树的直径

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const LL INF = 0x3f3f3f3f3f3f3f3f;
  5. const int maxn = ;
  6. struct arc {
  7. int v,w,next;
  8. arc(int y = ,int z = ,int nxt = -) {
  9. v = y;
  10. w = z;
  11. next = nxt;
  12. }
  13. bool operator<(const arc &t)const {
  14. return w < t.w;
  15. }
  16. } e[];
  17. int hd[maxn],hd2[maxn],low[maxn],dfn[maxn],belong[maxn],tot;
  18. void add(int *head,int u,int v,int w) {
  19. e[tot] = arc(v,w,head[u]);
  20. head[u] = tot++;
  21. e[tot] = arc(u,w,head[v]);
  22. head[v] = tot++;
  23. }
  24. int bcc,clk,n,m,uf[maxn];
  25. stack<int>stk;
  26. int Find(int x) {
  27. if(x != uf[x]) uf[x] = Find(uf[x]);
  28. return uf[x];
  29. }
  30. void tarjan(int u,int fa) {
  31. low[u] = dfn[u] = ++clk;
  32. stk.push(u);
  33. bool flag = false;
  34. for(int i = hd[u]; ~i; i = e[i].next) {
  35. if(!flag && e[i].v == fa) {
  36. flag = true;
  37. continue;
  38. }
  39. if(!dfn[e[i].v]) {
  40. tarjan(e[i].v,u);
  41. low[u] = min(low[u],low[e[i].v]);
  42. } else low[u] = min(low[u],dfn[e[i].v]);
  43. }
  44. if(low[u] == dfn[u]) {
  45. int v;
  46. ++bcc;
  47. do {
  48. v = stk.top();
  49. stk.pop();
  50. belong[v] = bcc;
  51. } while(v != u);
  52. }
  53. }
  54. LL d[][maxn];
  55. queue<int>q;
  56. int bfs(int u,int idx) {
  57. memset(d[idx],-,sizeof d[idx]);
  58. while(!q.empty()) q.pop();
  59. d[idx][u] = ;
  60. q.push(u);
  61. while(!q.empty()) {
  62. int u = q.front();
  63. q.pop();
  64. for(int i = hd2[u]; ~i; i = e[i].next) {
  65. if(d[idx][e[i].v] == -) {
  66. d[idx][e[i].v] = d[idx][u] + e[i].w;
  67. q.push(e[i].v);
  68. }
  69. }
  70. }
  71. LL ret = -;
  72. int id = ;
  73. for(int i = ; i <= bcc; ++i)
  74. if(ret < d[idx][i]) ret = d[idx][id = i];
  75. return id;
  76. }
  77. void init() {
  78. for(int i = ; i < maxn; ++i) {
  79. hd[i] = hd2[i] = -;
  80. low[i] = dfn[i] = belong[i] = ;
  81. uf[i] = i;
  82. }
  83. clk = tot = bcc = ;
  84. while(!stk.empty()) stk.pop();
  85. }
  86. int main() {
  87. int kase,u,v,w;
  88. scanf("%d",&kase);
  89. while(kase--) {
  90. init();
  91. scanf("%d%d",&n,&m);
  92. for(int i = ; i < m; ++i) {
  93. scanf("%d%d%d",&u,&v,&w);
  94. add(hd,u,v,w);
  95. }
  96. for(int i = ; i <= n; ++i)
  97. if(!dfn[i]) tarjan(i,-);
  98. if(bcc == ) {
  99. puts("1 0");
  100. continue;
  101. }
  102. for(int i = ; i <= n; ++i)
  103. for(int j = hd[i]; ~j; j = e[j].next) {
  104. if(belong[i] == belong[e[j].v]) continue;
  105. add(hd2,belong[i],belong[e[j].v],e[j].w);
  106. }
  107. bfs(bfs(bfs(,),),);
  108. LL ret = INF;
  109. int id = ;
  110. for(int i = ; i <= n; ++i) {
  111. int bg = belong[i];
  112. LL tmp = max(d[][bg],d[][bg]);
  113. if(tmp < ret) {
  114. ret = tmp;
  115. id = i;
  116. }
  117. }
  118. printf("%d %I64d\n",id,ret);
  119. }
  120. return ;
  121. }

CodeForcesGym 100676H Capital City的更多相关文章

  1. Gym - 100676H Capital City(边强连通分量 + 树的直径)

    H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...

  2. Gym - 100676H H. Capital City (边双连通分量缩点+树的直径)

    https://vjudge.net/problem/Gym-100676H 题意: 给出一个n个城市,城市之间有距离为w的边,现在要选一个中心城市,使得该城市到其余城市的最大距离最短.如果有一些城市 ...

  3. ACM Arabella Collegiate Programming Contest 2015 H. Capital City 边连通分量

    题目链接:http://codeforces.com/gym/100676/attachments 题意: 有 n 个点,m 条边,图中,边强连通分量之间可以直达,即距离为 0 ,找一个点当做首都,其 ...

  4. Gym100676 H. Capital City

    感觉题目都已经快把正解给说出来了...strongly connected的两个点的消耗为0,其实就是同一个边双连通分量里面的点消耗为0.然后缩一下点,再树形DP一下就完了.第一次写边双,但感觉挺简单 ...

  5. Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)

    A(By talker): 题意分析:以a(int) op b(int)形式给出两个整数和操作符, 求两个整数是否存在操作符所给定的关系 ,有则输出true,无则输出false: 思路:由于无时间复杂 ...

  6. Topcoder SRM590 Fox And City

    Problem Statement      There is a country with n cities, numbered 0 through n-1. City 0 is the capit ...

  7. Swift学习笔记-ARC

    Swift使用自动引用计数(ARC)机制来跟踪和管理你的应用程序的内存.通常情况下,Swift 内存管理机制会一直起作用,你无须自己来考虑内存的管理.ARC 会在类的实例不再被使用时,自动释放其占用的 ...

  8. Hdu 4081 最小生成树

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  9. blade and soul zone overview

    The world of Blade and Soul, is a vast extension of land containing two continents (the Southern Con ...

随机推荐

  1. Intent 使用方法全面总结

    调用拨号程序 // 给移动客服10086拨打电话 Uri uri = Uri.parse("tel:10086"); Intent intent = new Intent(Inte ...

  2. 2016.04.14,英语,《Vocabulary Builder》Unit 14

    crypt/cryph, comes from the Greek word for 'hidden', encrypt, crypto- crypt : [krɪpt] n. 土窖, 地穴, (教堂 ...

  3. 每天一个linux命令(01):ifconfig命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  4. poj--3624--Charm Bracelet(动态规划 水题)

    Home Problem Status Contest Add Contest Statistic LOGOUT playboy307 UPDATE POJ - 3624 Charm Bracelet ...

  5. B1108 [POI2007]天然气管道Gaz 贪心

    啊啊啊,这题有毒.我想了各种花式数据结构,最后告诉我贪心???受不了... 题干: Description Mary试图控制成都的天然气市场.专家已经标示出了最好的天然气井和中转站在成都的地图.现在需 ...

  6. 从默认析构函数学习c++,new,delete,内存泄漏,野指针

    默认析构函数:当系统没有显式定义析构函数,编译器同样会为对象定义一个默认析构函数,默认的析构函数只能释放普通数据成员所占用的空间,无法通过释放通过new和malloc进行申请的空间,因此避免内存泄漏, ...

  7. 关于打包压缩几种格式(gzip,bzip2,xz)的试验对比

    要通过脚本进行备份,必然将会应用到压缩技术,这里简单针对几个常见的格式进行测验,从而得到一种合适的方式. 这里以一个应用目录做例子: [root@isj-test-5 mnt]$du -sh * 66 ...

  8. javascript中caller和callee call和apply

    Arguments : 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[n] 参数function :选项.当前正在执行的 Function 对象的名字. n ...

  9. 第6章 服务模式 在 .NET 中实现 Service Interface

    上下文 您 的应用程序部署在 Microsoft Windows? 操作系统上.您决定将应用程序的某一块功能作为 ASP.NET Web Service 公开.互操作性是一个关键问题,因此您无法使用仅 ...

  10. vuejs开发H5页面总结

    最近参与了APP内嵌H5页面的开发,这次使用vuejs替代了jQuery,仅仅把vuejs当做一个库来使用,效率提高之外代码可读性更强,在此分享一下自己的一些开发中总结的经验. 关于布局方案 当拿到设 ...