求一个无向图的点双联通分量,大白书315页
性质:不同的点-双联通分量之间最多只有一个公共点,且它一定是割顶,反过来说任何割顶都至少是两个不同的点-双连通分量的公共点
模板
int n, m;
int dfs_clock, bcc_cnt;//bcc_cnt记录点-双连通分量的个数,初始化是0但是是从1开始的
int pre[maxn], low[maxn], bccno[maxn];//bccno[u]记录点u属于第几个双联通分量,即点u所在双联通分量的编号
bool iscut[maxn];
vector<int> g[maxn], bcc[maxn];//bcc[i]记录第i个双连通分量有哪些点
struct edge {
int u, v;
edge(int uu, int vv) :u(uu), v(vv) {}
};
stack<edge> s;//保存一个点-双联通分量中的边
int dfs(int u, int fa) {
int lowu = pre[u] = ++dfs_clock;
int child = 0;
for (int i = 0; i < g[u].size(); ++i) {
int v = g[u][i];
edge e = edge(u, v);
if (0 == pre[v]) {//没有访问过v
++child;
s.push(e);
int lowv = dfs(v, u);
lowu = min(lowu, lowv);//用后代的low更新自己
if (lowv >= pre[u]) {
iscut[u] = true;
++bcc_cnt;//从1开始
bcc[bcc_cnt].clear();
while (1) {
edge x = s.top();
s.pop();
if (bccno[x.u] != bcc_cnt) { bcc[bcc_cnt].push_back(x.u); bccno[x.u] = bcc_cnt; }
if (bccno[x.v] != bcc_cnt) { bcc[bcc_cnt].push_back(x.v); bccno[x.v] = bcc_cnt; }
if (x.u == u && x.v == v) break;
}
}
}
else if (pre[v] < pre[u] && fa != v) {
s.push(e);
lowu = min(lowu, pre[v]);
}
}
if (fa < 0 && child == 1) iscut[u] = false;
return low[u] = lowu;
}
void find_bcc(int n) {
memset(pre, 0, sizeof(pre));
memset(iscut, 0, sizeof(iscut));
memset(bccno, 0, sizeof(bccno));
dfs_clock = bcc_cnt = 0;
for (int i = 0; i < n; ++i) {
if (0 == pre[i]) dfs(i, -1);
}
}
源程序
#include<bits/stdc++.h>
using namespace std;
const int maxn = 150;
const int maxm = 1050;
int n, m;
int dfs_clock, bcc_cnt;//bcc_cnt记录点-双连通分量的个数,初始化是0但是是从1开始的
int pre[maxn], low[maxn], bccno[maxn];//bccno[u]记录点u属于第几个双联通分量,即点u所在双联通分量的编号
bool iscut[maxn];
vector<int> g[maxn], bcc[maxn];//bcc[i]记录第i个双连通分量有哪些点
void init() {
for (int i = 0; i < maxn; ++i) g[i].clear();
}
struct edge {
int u, v;
edge(int uu, int vv) :u(uu), v(vv) {}
};
stack<edge> s;//保存一个点-双联通分量中的边
int dfs(int u, int fa) {
int lowu = pre[u] = ++dfs_clock;
int child = 0;
for (int i = 0; i < g[u].size(); ++i) {
int v = g[u][i];
edge e = edge(u, v);
if (0 == pre[v]) {//没有访问过v
++child;
s.push(e);
int lowv = dfs(v, u);
lowu = min(lowu, lowv);//用后代的low更新自己
if (lowv >= pre[u]) {
iscut[u] = true;
++bcc_cnt;//从1开始
bcc[bcc_cnt].clear();
while (1) {
edge x = s.top();
s.pop();
if (bccno[x.u] != bcc_cnt) { bcc[bcc_cnt].push_back(x.u); bccno[x.u] = bcc_cnt; }
if (bccno[x.v] != bcc_cnt) { bcc[bcc_cnt].push_back(x.v); bccno[x.v] = bcc_cnt; }
if (x.u == u && x.v == v) break;
}
}
}
else if (pre[v] < pre[u] && fa != v) {
s.push(e);
lowu = min(lowu, pre[v]);
}
}
if (fa < 0 && child == 1) iscut[u] = false;
return low[u] = lowu;
}
void find_bcc(int n) {
memset(pre, 0, sizeof(pre));
memset(iscut, 0, sizeof(iscut));
memset(bccno, 0, sizeof(bccno));
dfs_clock = bcc_cnt = 0;
for (int i = 0; i < n; ++i) {
if (0 == pre[i]) dfs(i, -1);
}
}
int main() {
while (scanf("%d%d", &n, &m) == 2) {
init();
for (int i = 1; i <= m; ++i) {
int from, to;
scanf("%d%d", &from, &to);
g[from].push_back(to);
g[to].push_back(from);
}
find_bcc(n);
cout << "点-双连通分量一共有" << bcc_cnt << "个\n";
for (int i = 1; i <= bcc_cnt; ++i) {
sort(bcc[i].begin(), bcc[i].end());
cout << "第" << i << "个双连通分量包含如下结点\n";
for (int j = 0; j < bcc[i].size(); ++j)
cout << bcc[i][j] << " ";
cout << endl;
}
}
return 0;
}