Tarjan 全家桶
WRT_Partisan · · 算法·理论
强连通分量(SCC)
概念
强连通:有向图中,如果两点可以互相到达,那么这两点强连通。
强连通子图:子图中任意两点强连通。
强连通分量:极大强连通子图。
运用场景:强连通分量标记、将图缩点变成 DAG。
Kosaraju 算法
步骤:
- 正向图中搜索得到包含所有点的后序遍历序列。
- 将序列反转,依次在反图中进行强连通分量标记。
正确性证明:
- 强连通的两点,无论先搜后搜,都会被标记为相同值。
- 非强连通的两点,无论先搜后搜,都会标记为不同值。
若反图中
求强连通分量:
int scc[N], kcnt;
// scc 为原图每个点所在强连通分量编号,kcnt 为强连通分量数量
bool kbook[N];
// kbook 用来标记每个点是否已经被搜索到
vector<int> kmp[N], kfmp[N], kq;
// kmp 为原图,kfmp 为反图,kq 为后序遍历队列
void kdfs1(int x) // 正图搜索
{
kbook[x] = 1;
for (auto i : kmp[x])
if (!kbook[i])
kdfs1(i);
kq.push_back(x);
}
void kdfs2(int x, int id) // 反图标记
{
scc[x] = id;
for (auto i : kfmp[x])
if (!scc[i])
kdfs2(i, id);
}
void kosaraju() // 总函数
{
for (int i = 1; i <= n; i++)
if (!kbook[i])
kdfs1(i);
reverse(kq.begin(), kq.end());
for (auto i : kq)
if (!scc[i])
kdfs2(i, ++kcnt);
}
缩点:
int rd[N];
// rd 为每个强连通分量的入度
unordered_set<int> mp[N];
// mp 为缩点后的图,可能有重边,用 unordered_set 去重;或者可以用 vector 存边,排序后用 unique 去重
void get_graph() // 缩点
{
for (int i = 1; i <= n; i++)
for (auto j : kmp[i])
if (scc[i] != scc[j] && !mp[scc[i]].count(scc[j]))
mp[scc[i]].insert(scc[j]), rd[scc[j]]++;
}
Tarjan 求强连通分量
dfn:该点的 DFS 序。
low:能到达的最小 DFS 序。
scc:每个点所在的强连通分量编号。
Tarjan 本质上就是在 DFS 中维护 low 值和 scc 编号。
- 树边:继续搜索,然后用儿子的
low维护。 - 返祖边:用祖先的
dfn维护。 - 前向边:如果对应点没有
scc,那么用dfn维护。 - 横叉边:如果对应点没有
scc,那么用dfn维护。
求强连通分量:
int low[N], dfn[N], tar, ts[N], top, scc[N], cnt;
// low:能到达的最小 DFS 序,dfn:该点的 DFS 序,scc:每个点所在的强连通分量编号
vector<int> mp[N];
void tarjan(int x)
{
low[x] = dfn[x] = ++tar; // tar:DFS 序编号
ts[++top] = x; // 手工栈(防止爆栈)
for (auto i : mp[x])
{
if (dfn[i] == 0) // 树边
tarjan(i), low[x] = min(low[x], low[i]);
else if (scc[i] == 0) // 返祖边、未标记横叉边、未标记前向边
low[x] = min(low[x], dfn[i]);
}
if (dfn[x] == low[x]) // 找到一个强连通分量的顶点
{
cnt++; // cnt:新的强连通分量编号
int t;
do
{
t = ts[top--], scc[t] = cnt; // 弹出该强连通分量的点并编号
} while (t != x); // 直到弹到顶点
}
}
缩点:
int u[M], v[M];
// 需要将每条边都存起来
int rd[N], cd[N], num[N];
// rd 为每个强连通分量的入度,cd 为每个强连通分量的出度,num 为每个强连通分量中点的个数
vector<int> mpx[N];
// mpx 为缩点后的图
void get_graph() // 缩点
{
for (int i = 1; i <= n; i++)
if (scc[i] == 0)
tarjan(i);
for (int i = 1; i <= n; i++)
num[scc[i]]++;
for (int i = 1; i <= m; i++)
if (scc[u[i]] != scc[v[i]])
mpx[scc[u[i]]].push_back(scc[v[i]]), rd[scc[v[i]]]++, cd[scc[u[i]]]++;
}
割边
割边:无向图中删除该边后,极大连通分量增加。
将无向图看成有向图,不考虑反向边。
low[i] > dfn[x]:i没有其他路径能到x。low[i] <= dfn[x]:i有其他路径能到x,该边在一个环中。
struct Node
{
int v, id;
};
int dfn[N], low[N], tar, is_cut[M];
// is_cut 存储每条边是否是割边
vector<Node> mp[N];
void tarjan(int x, int e) // e 为来边编号,防止回搜
{
dfn[x] = low[x] = ++tar;
for (auto i : mp[x])
{
if (!dfn[i.v])
{
tarjan(i.v, i.id), low[x] = min(low[x], low[i.v]);
if (low[i.v] > dfn[x]) // 对于每条边,跑完后判断
is_cut[i.id] = 1;
}
else if (e != i.id) // 不走反边
low[x] = min(low[x], dfn[i.v]);
}
}
void get_cut() // 得到每条边是否为割边
{
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i, 0);
}
割点
割点:无向图中删除该点后,极大连通分量增加。
将无向图看成有向图,不考虑反向边。
low[i] >= dfn[x]:i没有经过x祖先点的路径能到x。low[i] < dfn[x]:i有经过x祖先点的路径能到x。
特殊情况:x 为根时其 DFS 树上儿子数大于
int dfn[N], low[N], tar, cnt, c[N];
// cnt 为割点数量,c 记录每个点是否为割点
vector<int> mp[N];
void tarjan(int x, int r) // r 为根编号
{
dfn[x] = low[x] = ++tar;
int s = 0; // s 记录该点的儿子个数
for (auto i : mp[x])
{
if (!dfn[i])
{
s++, tarjan(i, r), low[x] = min(low[x], low[i]);
if (low[i] >= dfn[x] && x != r) // x 非根
cnt += 1 - c[x], c[x] = 1; // 多个儿子,多次判断,去重
}
else
low[x] = min(low[x], dfn[i]);
}
if (s > 1 && x == r) // x 是根
cnt += 1 - c[x], c[x] = 1;
}
void get_cut() // 得到每个点是否为割点
{
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i, i);
}
边双连通分量
边双连通分量:不包含割边的极大连通子图。
边双具有传递性。若
Tarjan 强连通
注意只能判断回边不能判断回点,因为可能会有多重边的问题。
struct Node
{
int v, id;
};
int low[N], dfn[N], tar, ts[N], top, scc[N], cnt;
// low:能到达的最小 DFS 序,dfn:该点的 DFS 序,scc:每个点所在的边双连通分量编号
vector<Node> mp[N];
void tarjan(int x, int e) // e 为来边编号
{
dfn[x] = low[x] = ++tar, ts[++top] = x;
for (auto i : mp[x])
{
if (dfn[i.v] == 0) // 树边
tarjan(i.v, i.id), low[x] = min(low[x], low[i.v]);
else if (i.id != e) // 非回边
low[x] = min(low[x], dfn[i.v]);
}
if (dfn[x] == low[x]) // 找到一个边双连通分量的顶点
{
cnt++; // cnt:新的边双连通分量编号
int t;
do
{
t = ts[top--], scc[t] = cnt; // 弹出该边双连通分量的点并编号
} while (t != x); // 直到弹到顶点
}
}
void get() // 得到每个点所在边双连通分量编号
{
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i, 0);
}
点双连通分量
点双连通分量:不包含割点的极大连通子图。
可以使用边双的想法,遇到割点就把栈内部的点弹出,但是由于割点可能是公共点,因此需要特殊考虑。
struct Node
{
int v, id;
};
int low[N], dfn[N], tar, ts[N], top, cnt;
// low:能到达的最小 DFS 序,dfn:该点的 DFS 序
vector<Node> mp[N];
vector<int> scc[N];
// scc:每个点双连通分量中的点
void tarjan(int x, int e) // e 为来边编号
{
low[x] = dfn[x] = ++tar, ts[++top] = x;
int s = 0;
for (auto i : mp[x])
{
if (dfn[i.v] == 0)
{
s++, tarjan(i.v, i.id), low[x] = min(low[x], low[i.v]);
if (low[i.v] >= dfn[x]) // 割点 + 起点
{
cnt++;
int t;
do
{
t = ts[top--], scc[cnt].push_back(t);
} while (t != i.v); // 保留 x
scc[cnt].push_back(x); // 加入 x
}
}
else if (i.id != e)
low[x] = min(low[x], dfn[i.v]);
}
if (e == 0 && s == 0) // 孤立点
scc[++cnt].push_back(x);
}
void get() // 得到每个点双连通分量中的点
{
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i, 0);
}