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. Empower Developers

     Empower Developers Timothy High THingS ARE uSuAlly EASiER SAid THAn donE, and software architects ...

  2. 怎样在注冊表禁用或打开windows系统右键菜单

    以下是禁用右键方法: 在HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer下  在右边的窗体中新 ...

  3. URL重写及ASP.NET路由、Http处理模块、程序等

    这段时间在学习ASP.NET路由.HTTP处理等内容,了解了一些,但又未完全弄懂,似是而非,不管如何,作一总结,供日后借鉴和修改. 一.IIS6和IIS7经典模式和集成模式 在IIS6及IIS7经典模 ...

  4. C语言 - typedef struct 与struct

    c语言中可以选择的数据类型太少了. Java中有一些高级的数据结构. 结构中能够存放基本的数据类型以及其他的结构. 结构定义,一般放在程序的开头部分. 一般放在include之后. #include ...

  5. subprocess学习

    转自http://blog.csdn.net/imzoer/article/details/8678029 subprocess的目的就是启动一个新的进程并且与之通信. subprocess模块中只定 ...

  6. 深入理解 python 元类

    一.什么的元类 # 思考: # Python 中对象是由实例化类得来的,那么类又是怎么得到的呢? # 疑问: # python 中一切皆对象,那么类是否也是对象?如果是,那么它又是那个类实例化而来的呢 ...

  7. 大数据学习之路------借助HDP SANDBOX开始学习

    一开始... 一开始知道大数据这个概念的时候,只是感觉很高大上,引起了我的兴趣.当时也不知道,这个东西是做什么的,有什么用,当然现在看来也是很模糊的样子,但是的确比一开始强了不少. 所以学习的过程可能 ...

  8. Prism学习(1)---前期准备

    本文摘取自Gene's Blog的博客园文章,版权归Gene's Blog,仅供个人学习参考.转载请标明原作者Gene's Blog. 在学习Prism框架之前,我预先写了一个非常简单的计算器解决方案 ...

  9. 洛谷P2391 白雪皑皑(并查集)

    题目背景 “柴门闻犬吠,风雪夜归人”,冬天,不期而至.千里冰封,万里雪飘.空中刮起了鸭毛大雪.雪花纷纷,降落人间. 美能量星球(pty 在 spore 上的一个殖民地)上的人们被这美景所震撼.但是 p ...

  10. P1304 哥德巴赫猜想

    题目描述 输入N(N<=10000),验证4~N所有偶数是否符合哥德巴赫猜想. (N为偶数). 如果一个数,例如10,则输出第一个加数相比其他解法最小的方案.如10=3+7=5+5,则10=5+ ...