前言
今天考了一套$Claris$的题
$T1$就是$Claris$用$bitset$配合这个算法把求$SCC$优化到了$\frac{n^2}{32}$
吓得我赶紧来学了学
算法概述
$step \ 1.$ DFS一遍,记录下后序遍历的顺序
$step \ 2.$ 沿后续遍历的逆序、沿反向边再DFS一遍,每一个连通块即为一个SCC
算法解释
设原图为图$G$,将$SCC$缩点后的新图为$G’$
第一遍$DFS$保证了在若某个强连通分量$A$在$G’$中是另一个强连通分量$B$的祖先
那么在后续遍历的逆序中至少存在一个点$A$中的点在$B$中所有点的前面
这样在第二次$DFS$的时候,因为是逆序,所以只能往$G’$中的祖先走
又因为祖先的$SCC$已经标记过了,所以刚好把当前$SCC$搞完,不会有多
代码实现
int scc_cnt, vis[N]; vector<int> scc_cnt[N], que; void DFS0(int w) { vis[w] = 0; for (int i = head[w]; i; i = nxt[i]) { if (!vis[to[i]]) { DFS0(to[i]); } } que[++tot] = w; } void DFS1(int w) { scc[scc_cnt].push_back(w); vis[w] = 1; for (int i = rev_head[w]; i; i = nxt[i]) { if (!vis[to[i]]) { DFS1(to[i]); } } } void solve() { que.clear(); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { if (!vis[i]) { DFS0(i); } } scc_cnt = 0; memset(vis, 0, sizeof(vis)); for (int i = (int)que.size() - 1; ~i; i--) { if (!vis[que[i]]) { scc_cnt++; DFS1(que[i]); } } }
使用$bitset$优化
这货复杂度是$O(m)$的
不难发现算法瓶颈在查找与某个点相邻的所有点中还没有被访问过的点
这个可以用$bitset$压位并配合__builtin
开头的系列函数优化到$O(\frac{n^2}{32})$
例题的话,可以参考:友好城市
Hey there, You’ve done a great job. I will certainly digg it and in my view suggest to my friends. I am confident they will be benefited from this site.
I like what you guys are up too. Such clever work and reporting! Keep up the superb works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my web site 🙂