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. vmware centos 连网方式

    1.自动获取IP地址 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分配IP地址. #dhclient 自动获取ip地址命令 #ifconfig 查询系统里网卡信息,i ...

  2. 剑指offer--10.最小的K个数

    边界判断,坑了一下 ----------------------------------------------- 时间限制:1秒 空间限制:32768K 热度指数:375643 本题知识点: 数组 ...

  3. flash、js 函数 互相调用

    js调用flex函数 flex 利用ExternalInterface.addCallback(“注册的方法名”,As中的函数名)进行注册 js中,用document.getElementById(“ ...

  4. java-10异常处理动手动脑

    1.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. import javax.swing.*; class AboutExcep ...

  5. 软件架构设计 ADMEMS方法体系

    ADMEMS是Architecture Design Method has been Extended to Method System的简称,是由CSAI顾问团架构设计专家组于2009年11月在第六 ...

  6. poj 2262 Goldbach's Conjecture——筛质数(水!)

    题目:http://poj.org/problem?id=2262 大水题的筛质数. #include<iostream> #include<cstdio> #include& ...

  7. poj 2096 , zoj 3329 , hdu 4035 —— 期望DP

    题目:http://poj.org/problem?id=2096 题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种: 所以设 f[i][j] 表示已经有几种 ...

  8. Poj 1321 棋盘问题(搜索)

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  9. H264码流结构分析和rtp打包结构详解

    网络抽象层单元类型 (NALU): NALU头由一个字节组成,它的语法如下: +---------------+      |0|1|2|3|4|5|6|7|      +-+-+-+-+-+-+-+ ...

  10. SEO中常用HTML代码大全,及权重排序

    做SEO必须要懂HTML,说的是一点都没错,不过其实是不需要全部都懂,最重点的你懂了会用,基本上都是事半功倍了.可以这么说一个不懂代码的优化人员不算是一个合格的好优化.下面就总结一下做优化,必须要懂得 ...