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 ...
随机推荐
- ssh无法连接到远端Ubuntu的解决方法
近日,饱受无法远程登录到新安装在VMWare上的Ubuntu虚拟机,现在发现问题所在.故记录此问题的解决方式,以备后用. 一.远程登录虚拟机的准备: Ubuntu虚拟机的联网方式应该选择Bridged ...
- 输入URL 一瞬间发生了什么
当你在浏览器中输入url后发生了什么?下面是个人学习过程中的总结,如有理解不正确或不足的地方希望大家指出.先上一张脑图: 还有个问题:www.baidu.com 键入后,域名怎么知道的是这个IP! 补 ...
- mvc:view-controller直接转发页面
在springMVC中,通过@RequestMapping发送请求地址,转发到目标页面,但是,有时候想直接访问页面, 不想通过xxx.jsp直接访问页面,可以通过springmvc.xml配置文件中的 ...
- php递归取目录下的所有文件(原创)
function get_dir_all_files($path) { $result=array(); $temp=array(); if(filetype($path)=='dir') { $di ...
- 谈谈JavaScript深浅拷贝
浅拷贝 function shallowCopy(source){ var newObj = {}; for(var attr in source){ newObj[attr] = source[at ...
- 11g Rac PSU20180116手动补丁升级步骤
手动升级:软件包解压在新建的/home/grid/update 目录下ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1GRID_HOME=/u01 ...
- Nginx介绍及知识点(摘抄)
正向代理是把自己的网络环境切换成代理的网络 反向代理是代理机器返回给我要我的资源 本文借鉴参考于http://tengine.taobao.org/book/chapter_02.html. 属于纯干 ...
- 完美解决ios10及以上Safari无法禁止缩放的问题
移动端web缩放有两种: 1.双击缩放: 2.双指手势缩放. 在iOS 10以前,iOS和Android都可以通过一行meta标签来禁止页面缩放 <meta content="widt ...
- jQuery基本选择器模块
选择器模块 1.获取元素的基本操作 案例:给页面中的div和p设置边框样式 1.1 传统方式 -获取元素并设置样式 实现思路 1 通过 标签名 获取元素 2 遍历循环 设置样式 var dvs = d ...
- java控制台输入输出字符串
一.实例说明 本实例通过输入流(System.in)实现从控制台接受用户输入信息,并将该信息输出到控制台. 运行效果如下图: 二.实现代码 三.要点说明 该实例的关键就是用到了System类的输入流, ...