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

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

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = ;
struct arc {
int v,w,next;
arc(int y = ,int z = ,int nxt = -) {
v = y;
w = z;
next = nxt;
}
bool operator<(const arc &t)const {
return w < t.w;
}
} e[];
int hd[maxn],hd2[maxn],low[maxn],dfn[maxn],belong[maxn],tot;
void add(int *head,int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
e[tot] = arc(u,w,head[v]);
head[v] = tot++;
}
int bcc,clk,n,m,uf[maxn];
stack<int>stk;
int Find(int x) {
if(x != uf[x]) uf[x] = Find(uf[x]);
return uf[x];
}
void tarjan(int u,int fa) {
low[u] = dfn[u] = ++clk;
stk.push(u);
bool flag = false;
for(int i = hd[u]; ~i; i = e[i].next) {
if(!flag && e[i].v == fa) {
flag = true;
continue;
}
if(!dfn[e[i].v]) {
tarjan(e[i].v,u);
low[u] = min(low[u],low[e[i].v]);
} else low[u] = min(low[u],dfn[e[i].v]);
}
if(low[u] == dfn[u]) {
int v;
++bcc;
do {
v = stk.top();
stk.pop();
belong[v] = bcc;
} while(v != u);
}
}
LL d[][maxn];
queue<int>q;
int bfs(int u,int idx) {
memset(d[idx],-,sizeof d[idx]);
while(!q.empty()) q.pop();
d[idx][u] = ;
q.push(u);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = hd2[u]; ~i; i = e[i].next) {
if(d[idx][e[i].v] == -) {
d[idx][e[i].v] = d[idx][u] + e[i].w;
q.push(e[i].v);
}
}
}
LL ret = -;
int id = ;
for(int i = ; i <= bcc; ++i)
if(ret < d[idx][i]) ret = d[idx][id = i];
return id;
}
void init() {
for(int i = ; i < maxn; ++i) {
hd[i] = hd2[i] = -;
low[i] = dfn[i] = belong[i] = ;
uf[i] = i;
}
clk = tot = bcc = ;
while(!stk.empty()) stk.pop();
}
int main() {
int kase,u,v,w;
scanf("%d",&kase);
while(kase--) {
init();
scanf("%d%d",&n,&m);
for(int i = ; i < m; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(hd,u,v,w);
}
for(int i = ; i <= n; ++i)
if(!dfn[i]) tarjan(i,-);
if(bcc == ) {
puts("1 0");
continue;
}
for(int i = ; i <= n; ++i)
for(int j = hd[i]; ~j; j = e[j].next) {
if(belong[i] == belong[e[j].v]) continue;
add(hd2,belong[i],belong[e[j].v],e[j].w);
}
bfs(bfs(bfs(,),),);
LL ret = INF;
int id = ;
for(int i = ; i <= n; ++i) {
int bg = belong[i];
LL tmp = max(d[][bg],d[][bg]);
if(tmp < ret) {
ret = tmp;
id = i;
}
}
printf("%d %I64d\n",id,ret);
}
return ;
}

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. Codeforces Round #377 (Div. 2) D. Exams

    Codeforces Round #377 (Div. 2) D. Exams    题意:给你n个考试科目编号1~n以及他们所需要的复习时间ai;(复习时间不一定要连续的,可以分开,只要复习够ai天 ...

  2. 64bit Centos6.4搭建hadoop-2.5.1

    64bit Centos6.4搭建hadoop-2.5.1 1.分布式环境搭建 採用4台安装Linux环境的机器来构建一个小规模的分布式集群. 当中有一台机器是Master节点,即名称节点,另外三台是 ...

  3. 【五】注入框架RoboGuice使用:(Your First POJO Injection)

    上一篇我们简单的介绍了一下RoboGuice的使用([四]注入框架RoboGuice使用:(Your First System Service Injection)),今天我们来看下普通Java对象的 ...

  4. 利用LruCache载入网络图片实现图片瀑布流效果(改进版)

    PS: 2015年1月20日21:37:27 关于LoadImageAsyncTask和checkAllImageViewVisibility可能有点小bug 改动后的代码请參见升级版本号的代码 ht ...

  5. VS2013+ffmpeg开发环境搭建-->【转】

    本文转载自:http://blog.csdn.net/qq_28425595/article/details/51488869 版权声明:本文为博主原创文章,未经博主允许不得转载. 今天整理资料时,发 ...

  6. Linux - 控制台界面,虚拟界面,字符界面

    tty控制台终端. pts虚拟终端. tty1 图形界面. tty2 字符界面. Ctrl+Alt+F2-6 在字符界面下,通过Alt+F2 切换回来.或者切换到其他的字符界面. Alt+F2 pts ...

  7. 循环神经网络(RNN, Recurrent Neural Networks)——无非引入了环,解决时间序列问题

    摘自:http://blog.csdn.net/heyongluoyao8/article/details/48636251 不同于传统的FNNs(Feed-forward Neural Networ ...

  8. package-org.springframework.ui-interface:Model.class

    ylbtech-package-org.springframework.ui-interface:Model.class 1.返回顶部 1. /* * Copyright 2002-2012 the ...

  9. js 数组包含

    function(arr,element){ return new RegExp('(^|,)'+element.toString()+'(,|$)').test(arr.toString()); }

  10. strlen和mb_strlen

    在PHP中,strlen与mb_strlen是求字符串长度的函数,但是对于一些初学者来说,如果不看手册,也许不太清楚其中的区别.下面通过例子,讲解这两者之间的区别. 先看例子: <?php // ...