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 ...
随机推荐
- luogu2261 [CQOI2007] 余数之和
题目大意 求 \[\sum_{i=1}^{n}(k\mod i)\] \(n,k\leq 10^9\). 题解 先只考虑\(n\leq k\)的情况. \[\sum_{i=1}^{n}(k\mod i ...
- luogu2754 星际转移问题 网络流
题目大意:地球与月球间有可容纳无限人的太空站,还有在太空站与星球间按周期行驶的.有固定容量的太空船,每一艘太空船从一个太空站驶往任一太空站耗时均为 1.地球上有一定数量的人,问所有人到月球最少需要多少 ...
- global cache cr request
当一个进程访问需要一个或者多个块时,它会首先检查自己的CACHE是否存在该块,如果发现没有,就会先通过global cache赋予这些块 共享访问的权限,然后再访问.假如,通过global cache ...
- [JXOI 2018] 游戏 解题报告 (组合数+埃氏筛)
interlinkage: https://www.luogu.org/problemnew/show/P4562 description: solution: 注意到$l=1$的时候,$t(p)$就 ...
- Gym-101915D Largest Group 最大独立集 Or 状态压缩DP
题面题意:给你N个男生,N个女生,男生与男生之间都是朋友,女生之间也是,再给你m个关系,告诉你哪些男女是朋友,最后问你最多选几个人出来,大家互相是朋友. N最多为20 题解:很显然就像二分图了,男生一 ...
- Linux Shell Scripting Cookbook 读书笔记 7
ping, du, ps, kill, 收集系统信息 判断网络中哪些主机是活动主机 #!/bin/bash for ip in 10.215.70.{1..255}; do ( ping $ip -c ...
- 抽象类(abrstract class)与接口(interface)有何异同
抽象类:如果一个类中包含抽象方法(用abstract修饰的方法),那么这个类就是抽象类 接口:是指一个方法的集合,接口中的所有方法都没有方法体 相同点: 1)都不能被实例化 2)接口的实现类或抽象类的 ...
- OCC 矩阵变换
在OpenCADCADE中, 通过gp_Trsf类来进行矩阵变换操作, 采用矩阵在左的方式: 新点 = 变换矩阵 * 点 基本原理如下: //! Defines a non-persistent tr ...
- APP开发中,如何从UI设计上提升APP用户体验
设计中有很多细微的东西要注意,就如UI设计中,元素的统一性,图标风格.段落的排版等等,只有能注意这些细节,你的 APP UI 才算合格. 干货君总结了17个提升用户体验的 UI 设计小技巧,也是我们日 ...
- java 抽象工厂模式简单实例
抽象工厂模式:提供一个创建一系列的相关的或者依赖的对象的接口,无需指定它们的具体实现类,具体的时间分别在子类工厂中产生. 类似于工厂模式:隔离了具体类的生产实现,使得替换具体的工厂实现类很容易.包含有 ...