CodeForcesGym 100676H Capital City
H. Capital City
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的更多相关文章
- Gym - 100676H Capital City(边强连通分量 + 树的直径)
H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...
- Gym - 100676H H. Capital City (边双连通分量缩点+树的直径)
https://vjudge.net/problem/Gym-100676H 题意: 给出一个n个城市,城市之间有距离为w的边,现在要选一个中心城市,使得该城市到其余城市的最大距离最短.如果有一些城市 ...
- ACM Arabella Collegiate Programming Contest 2015 H. Capital City 边连通分量
题目链接:http://codeforces.com/gym/100676/attachments 题意: 有 n 个点,m 条边,图中,边强连通分量之间可以直达,即距离为 0 ,找一个点当做首都,其 ...
- Gym100676 H. Capital City
感觉题目都已经快把正解给说出来了...strongly connected的两个点的消耗为0,其实就是同一个边双连通分量里面的点消耗为0.然后缩一下点,再树形DP一下就完了.第一次写边双,但感觉挺简单 ...
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)
A(By talker): 题意分析:以a(int) op b(int)形式给出两个整数和操作符, 求两个整数是否存在操作符所给定的关系 ,有则输出true,无则输出false: 思路:由于无时间复杂 ...
- Topcoder SRM590 Fox And City
Problem Statement There is a country with n cities, numbered 0 through n-1. City 0 is the capit ...
- Swift学习笔记-ARC
Swift使用自动引用计数(ARC)机制来跟踪和管理你的应用程序的内存.通常情况下,Swift 内存管理机制会一直起作用,你无须自己来考虑内存的管理.ARC 会在类的实例不再被使用时,自动释放其占用的 ...
- Hdu 4081 最小生成树
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- blade and soul zone overview
The world of Blade and Soul, is a vast extension of land containing two continents (the Southern Con ...
随机推荐
- Git与SVN区别 \git学习
http://blog.csdn.net/w13770269691/article/details/38705473/ https://git-scm.com/book/zh/v2
- zoj3886--Nico Number(素数筛+线段树)
Nico Number Time Limit: 2 Seconds Memory Limit: 262144 KB Kousaka Honoka and Minami Kotori are ...
- BZOJ 4517: [Sdoi2016]排列计数 错排+逆元
4517: [Sdoi2016]排列计数 Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i, ...
- luogu3769 【模板】AC自动机(加强版)
题目大意:有N个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串T中出现的次数最多. 对每个模式串建立一个Trie树.定义一个节点的Fail指针 ...
- POJ3177 Redundant Paths 图的边双连通分量
题目大意:问一个图至少加多少边能使该图的边双连通分量成为它本身. 图的边双连通分量为极大的不存在割边的子图.图的边双连通分量之间由割边连接.求法如下: 求出图的割边 在每个边双连通分量内Dfs,标记每 ...
- linux下的oom调试笔记【原创】
平台信息:内核:linux3.0.68 系统:android/android5.1平台:s5p4418 作者:庄泽彬(欢迎转载,请注明作者) 邮箱:2760715357@qq.com 摘要:调整and ...
- 多个submit
<html><body> <form action="welcome.php" method="post">Name: &l ...
- 使用 `ConfigMap` 挂载配置文件
使用 ConfigMap 挂载配置文件 Intro 有一些敏感信息比如数据库连接字符串之类的出于安全考虑,这些敏感信息保存在了 Azure KeyVault 中,最近应用上了 k8s 部署,所以想把 ...
- POJ 3258 (NOIP2015 D2T1跳石头)
河中跳房子 总时间限制: 1000ms 内存限制: 65536kB 描述 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一个岩石跳到另一个岩石.这项激动人心的活动在一条长长的笔直河道中进行, ...
- WPF使用Winform PDFView控件
最近开发wpf项目中有一个模块需要显示PDF文件内容.由于WPF本身没有PDF加载控件(似乎有收费的我查到过类似的资料.如果有新的pdf控件也请通知我一下谢谢). 项目使用之前也是从网上获取的资料,因 ...