CodeForcesGym 100676H Capital City

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 = 100010;
  6 struct arc {
  7     int v,w,next;
  8     arc(int y = 0,int z = 0,int nxt = -1) {
  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[1000010];
 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[2][maxn];
 55 queue<int>q;
 56 int bfs(int u,int idx) {
 57     memset(d[idx],-1,sizeof d[idx]);
 58     while(!q.empty()) q.pop();
 59     d[idx][u] = 0;
 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] == -1) {
 66                 d[idx][e[i].v] = d[idx][u] + e[i].w;
 67                 q.push(e[i].v);
 68             }
 69         }
 70     }
 71     LL ret = -1;
 72     int id = 0;
 73     for(int i = 1; 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 = 0; i < maxn; ++i) {
 79         hd[i] = hd2[i] = -1;
 80         low[i] = dfn[i] = belong[i] = 0;
 81         uf[i] = i;
 82     }
 83     clk = tot = bcc = 0;
 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 = 0; i < m; ++i) {
 93             scanf("%d%d%d",&u,&v,&w);
 94             add(hd,u,v,w);
 95         }
 96         for(int i = 1; i <= n; ++i)
 97             if(!dfn[i]) tarjan(i,-1);
 98         if(bcc == 1) {
 99             puts("1 0");
100             continue;
101         }
102         for(int i = 1; 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(1,0),0),1);
108         LL ret = INF;
109         int id = 0;
110         for(int i = 1; i <= n; ++i) {
111             int bg = belong[i];
112             LL tmp = max(d[0][bg],d[1][bg]);
113             if(tmp < ret) {
114                 ret = tmp;
115                 id = i;
116             }
117         }
118         printf("%d %I64d
",id,ret);
119     }
120     return 0;
121 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4751073.html