V-DCC
)
点双连通分量(原理
见《进阶指南》第402
页。
模板题
#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int N = 50010, M = 300010;
struct edge {
int to, next;
};
edge e[2 * M];
int idx, head[N];
int n, m, root;
int cnt, dfn[N], low[N];
bool cut[N];
stack<int> s;
int tot;
vector<int> dcc[N];
void add_edge (int u, int v) {
e[idx].to = v;
e[idx].next = head[u];
head[u] = idx ++;
}
void tarjan (int cur) {
low[cur] = dfn[cur] = ++ cnt;
s.push(cur);
if (cur == root && head[cur] == -1) { // 孤立点
dcc[++ tot].push_back(cur);
return;
}
int sum = 0;
for (int i = head[cur]; i != -1; i = e[i].next) {
int to = e[i].to;
if (dfn[to] == 0) {
tarjan(to);
low[cur] = min(low[cur], low[to]);
if (low[to] >= dfn[cur]) {
++ sum;
if (cur != root || sum >= 2)
cut[cur] = true;
++ tot;
int t;
do {
t = s.top();
s.pop();
dcc[tot].push_back(t);
} while (t != to);
dcc[tot].push_back(cur);
}
} else {
low[cur] = min(low[cur], dfn[to]);
}
}
}
int main () {
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 1, u, v; i <= m; ++ i) {
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
for (int i = 1; i <= n; ++ i) {
if (dfn[i] == 0) {
root = i;
tarjan(i);
}
}
for (int i = 1; i <= tot; ++ i) {
for (int j = 0; j < dcc[i].size(); ++ j)
printf("%d ", dcc[i][j]);
printf("\n");
}
return 0;
}