CH24C 逃不掉的路
edcc缩点之后跳倍增lca
丢个edcc缩点模板
Code:
#include <cstdio>
#include <cstring>
using namespace std; const int N = 1e5 + ;
const int M = 2e5 + ;
const int Lg = ; int n, m, qn, tot = , head[N], dep[N], fa[N][Lg];
int dfsc = , dcc = , dfn[N], low[N], bel[N], ex[M], ey[M];
bool bri[M << ], vis[N]; struct Edge {
int to, nxt;
} e[M << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int min(int x, int y) {
return x > y ? y : x;
} void tarjan(int x, int inEdge) {
dfn[x] = low[x] = ++dfsc;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(!dfn[y]) {
tarjan(y, i);
low[x] = min(low[x], low[y]);
if(dfn[x] < low[y]) bri[i] = bri[i ^ ] = ;
} else if(i != (inEdge ^ ))
low[x] = min(low[x], dfn[y]);
}
} void dfs(int x) {
bel[x] = dcc;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(bel[y] || bri[i]) continue;
dfs(y);
}
} void dfs2(int x, int fat, int depth) {
vis[x] = , fa[x][] = fat, dep[x] = depth;
for(int i = ; i <= ; i++)
fa[x][i] = fa[fa[x][i - ]][i - ];
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(vis[y]) continue;
dfs2(y, x, depth + );
}
} void swap(int &x, int &y) {
int t = x;
x = y;
y = t;
} inline int getLca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = ; i >= ; i--)
if(dep[fa[x][i]] >= dep[y])
x = fa[x][i];
if(x == y) return x;
for(int i = ; i >= ; i--)
if(fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
return fa[x][];
} int main() {
read(n), read(m);
for(int i = ; i <= m; i++) {
read(ex[i]), read(ey[i]);
add(ex[i], ey[i]), add(ey[i], ex[i]);
} for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(i, );
for(int i = ; i <= n; i++)
if(!bel[i]) {
++dcc;
dfs(i);
} /* for(int i = 1; i <= n; i++)
printf("%d ", bel[i]);
printf("\n"); */ tot = ;
memset(head, , sizeof(head));
for(int i = ; i <= m; i++) {
if(bel[ex[i]] == bel[ey[i]]) continue;
add(bel[ex[i]], bel[ey[i]]), add(bel[ey[i]], bel[ex[i]]);
}
for(int i = ; i <= n; i++)
if(!vis[i]) dfs2(i, , ); read(qn);
for(int x, y; qn--; ) {
read(x), read(y);
x = bel[x], y = bel[y];
if(x == y) puts("");
else printf("%d\n", dep[x] + dep[y] - * dep[getLca(x, y)]);
}
return ;
}
CH24C 逃不掉的路的更多相关文章
- CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System
逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...
- ContestHunter#24-C 逃不掉的路
Description: 求无向图的必经边 思路:一眼题 将无向图缩成树,然后求两点树上距离 #include<iostream> #include<vector> #incl ...
- CH Round #24 - 三体杯 Round #1-C 逃不掉的路
留个e-DCC的板子 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...
- 高并发第九弹:逃不掉的Map --> HashMap,TreeMap,ConcurrentHashMap
平时大家都会经常使用到 Map,面试的时候又经常会遇到问Map的,其中主要就是 ConcurrentHashMap,在说ConcurrentHashMap.我们还是先看一下, 其他两个基础的 Map ...
- 逃不掉的mysql数据库安装方式大全yum rpm 源码
数据库虽然也不是天天安装,但每次安装都要找来找去挺烦,特整理记录在此. 系统基于:Centos 7.x 数据库版本: MySQL 5.7.x 转载请注明出处 Yum 安装方式 1.下载 yum rep ...
- Spring Cloud实战 | 第十篇 :Spring Cloud + Seata 1.4.1 + Nacos1.4.0 整合实现微服务架构中逃不掉的话题分布式事务
Seata分布式事务在线体验地址:https://www.youlai.store 本篇完整源码地址:https://github.com/hxrui/youlai-mall 有想加入开源项目开发的童 ...
- 『Tarjan算法 无向图的双联通分量』
无向图的双连通分量 定义:若一张无向连通图不存在割点,则称它为"点双连通图".若一张无向连通图不存在割边,则称它为"边双连通图". 无向图图的极大点双连通子图被 ...
- D8 双连通分量
记得有个梗那一天,zw学生zzh大佬说逃不掉的路变成a不掉的题哈哈哈哈: 分离的路径: BZOJ 1718POJ 3177LUOGU 286: 思路:在同一个边双连通分量中,任意两点都有至少两条独立路 ...
- P6跨级晋升P8再到P10,我的11年成长之路
来自:语雀,作者:玉伯 链接:https://www.yuque.com/yubo/morning/grow-up-at-alibaba 注:这是在阿里内部前端大学的一个分享,整理了一份对外的版本,希 ...
随机推荐
- 如何用visual studio2013编写简单C语言程序
vc++6.0 作为经典版本,虽然已经几乎淘汰,但还是有很多的初学者在使用.但当他们使用vs2013时会发现界面和操作和vc++6.0有了极大的不同,不知该如何 操作.随着vs2013的普及,更多人使 ...
- notepad++怎么显示项目的目录树?
转:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1013/1762.html
- Unity项目UI图片压缩格式(UGUI)
http://blog.csdn.net/bobodan123/article/details/70316538 UI制作时候使用的是Ps 8位 RGB通道的色彩. 但导出的是16位RGBA色彩的图片 ...
- Python函数-bytearray()
bytearray([source [, encoding [, errors]]]) bytearray([source [, encoding [, errors]]])返回一个byte数组.By ...
- python数据类型,int,str,bool
一,python中的int() int在python中主要用来运算,对字符串的转化,用int(str)表示,并且需要str.isdigit为真. 在int()中二进制的转换如下: #bit_lengt ...
- Linux应用函数 -- 字符串
1.strchr 原型 char *strchr(const char* _Str,char _Val) 头文件 string.h 功能 查找字符串_Str中首次出现字符_Val的位置 返回值 成功 ...
- XSS自动化检测 Fiddler Watcher & x5s & ccXSScan 初识
一.标题:XSS 自动化检测 Fiddler Watcher & x5s & ccXSScan 初识 automated XSS testing assistant 二.引言 ...
- 校赛热身 Problem C. Sometimes Naive (状压dp)
题解: 列举每一种3的倍数的组合,开始先求出3条边的可行解,则 六条边的可行解可以由两个三条边得来. 详见代码解析 #include<bits/stdc++.h> using namesp ...
- Regexp:目录
ylbtech-Regexp:目录 1.返回顶部 1. http://www.runoob.com/regexp/regexp-tutorial.html 2. 2.返回顶部 3.返回顶部 4 ...
- 刷题常用的STL容器总结
本文归纳总结刷题常用到STL容器以及一些标准算法,主要包括: string.vector.map.pair.unordered_map.set.queue.priority_queue.stack,以 ...