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 逃不掉的路的更多相关文章

  1. CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System

    逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...

  2. ContestHunter#24-C 逃不掉的路

    Description: 求无向图的必经边 思路:一眼题 将无向图缩成树,然后求两点树上距离 #include<iostream> #include<vector> #incl ...

  3. CH Round #24 - 三体杯 Round #1-C 逃不掉的路

    留个e-DCC的板子 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...

  4. 高并发第九弹:逃不掉的Map --> HashMap,TreeMap,ConcurrentHashMap

    平时大家都会经常使用到 Map,面试的时候又经常会遇到问Map的,其中主要就是 ConcurrentHashMap,在说ConcurrentHashMap.我们还是先看一下, 其他两个基础的 Map ...

  5. 逃不掉的mysql数据库安装方式大全yum rpm 源码

    数据库虽然也不是天天安装,但每次安装都要找来找去挺烦,特整理记录在此. 系统基于:Centos 7.x 数据库版本: MySQL 5.7.x 转载请注明出处 Yum 安装方式 1.下载 yum rep ...

  6. Spring Cloud实战 | 第十篇 :Spring Cloud + Seata 1.4.1 + Nacos1.4.0 整合实现微服务架构中逃不掉的话题分布式事务

    Seata分布式事务在线体验地址:https://www.youlai.store 本篇完整源码地址:https://github.com/hxrui/youlai-mall 有想加入开源项目开发的童 ...

  7. 『Tarjan算法 无向图的双联通分量』

    无向图的双连通分量 定义:若一张无向连通图不存在割点,则称它为"点双连通图".若一张无向连通图不存在割边,则称它为"边双连通图". 无向图图的极大点双连通子图被 ...

  8. D8 双连通分量

    记得有个梗那一天,zw学生zzh大佬说逃不掉的路变成a不掉的题哈哈哈哈: 分离的路径: BZOJ 1718POJ 3177LUOGU 286: 思路:在同一个边双连通分量中,任意两点都有至少两条独立路 ...

  9. P6跨级晋升P8再到P10,我的11年成长之路

    来自:语雀,作者:玉伯 链接:https://www.yuque.com/yubo/morning/grow-up-at-alibaba 注:这是在阿里内部前端大学的一个分享,整理了一份对外的版本,希 ...

随机推荐

  1. 场景中GameObject无法用代码隐藏问题(setActive为false)

    GameObject不受代码控制隐藏问题:代码中若对某个gameobject.setActive(false),发现会不起作用,总结下来发现是和object所在模型组的Animator组件的anima ...

  2. CodeForces - 1025D: Recovering BST (区间DP)

    Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! ...

  3. Scala 面向接口编程

    1.如果要实现一个接口,前边没有extends关键字就可以使用extends,如果有要使用with关键字 2.Scala 中的接口支持多种继承,类或者抽象类不支持多种继承 3.抽象属性:未被实例化的属 ...

  4. 1151 LCA in a Binary Tree(30 分)

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  5. Unity3D的SystemInfo类,用于获取运行设备硬件信息(CPU、显卡、类型等)

    SystemInfo类中的静态变量:   中文显示: Rendering.CopyTextureSupport copyTextureSupport:(只读)支持多种复制纹理功能的情况. string ...

  6. 自编jQuery插件实现模拟alert和confirm

    现在绝大多数网站都不用自带的alert和confirm了,因为界面太生硬了.因此这个插件就这样产生了自己定制一个的想法...... 啥也不说,先上图,有图有真相 :) 现在绝大多数网站都不用自带的al ...

  7. bzoj 3527 [Zjoi2014]力——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 把 q[ i ] 除掉.设 g[ i ] = i^2 ,有一半的式子就变成卷积了:另一 ...

  8. 解决方案: the selected file is a solution file but was created by a newer version of this application and cannot be opened

    最近在用IronGithub访问Github api时遇到一个问题: the selected file is a solution file but was created by a newer v ...

  9. git ssh登录

    .ssh下创建config文件 其内容: Host ipIdentityFile ~/.ssh/私钥文件名Port 22User git服务器的账号,不是git账号名(私钥文件名才是git账号名) 注 ...

  10. MySQL 使用pt-table-checksum 检查主从数据一致性 (实例转)

    1.基本环境: Mysql版本:5.6.12-log Percona-toolkit:2.2.18 Linux:centos6.5 2.安装 源码安装: # 一些依赖包 yum install per ...