You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.6 KiB
65 lines
1.6 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 5e5 + 10, M = 4e6 + 10;
|
|
|
|
int e[M], h[N], idx, w[M], ne[M];
|
|
void add(int a, int b, int c = 0) {
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
}
|
|
|
|
// 点双模板
|
|
int dfn[N], low[N], stk[N], ts, top, root;
|
|
vector<int> bcc[N];
|
|
int bcnt;
|
|
|
|
void tarjan(int u, int fa) {
|
|
low[u] = dfn[u] = ++ts;
|
|
stk[++top] = u;
|
|
int son = 0;
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int v = e[i];
|
|
if (v == fa) continue;
|
|
if (!dfn[v]) {
|
|
son++;
|
|
tarjan(v, u);
|
|
low[u] = min(low[u], low[v]);
|
|
|
|
if (low[v] >= dfn[u]) {
|
|
int x;
|
|
bcnt++;
|
|
do {
|
|
x = stk[top--];
|
|
bcc[bcnt].push_back(x);
|
|
} while (x != v); // 将子树出栈
|
|
bcc[bcnt].push_back(u); // 把割点/树根也丢到点双里
|
|
}
|
|
} else
|
|
low[u] = min(low[u], dfn[v]);
|
|
}
|
|
// 特判独立点
|
|
if (fa == -1 && son == 0) bcc[++bcnt].push_back(u);
|
|
}
|
|
|
|
int n, m;
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
scanf("%d %d", &n, &m);
|
|
while (m--) {
|
|
int a, b;
|
|
scanf("%d %d", &a, &b);
|
|
if (a != b) add(a, b), add(b, a);
|
|
}
|
|
|
|
for (root = 1; root <= n; root++)
|
|
if (!dfn[root]) tarjan(root, -1);
|
|
// 个数
|
|
printf("%d\n", bcnt);
|
|
for (int i = 1; i <= bcnt; i++) {
|
|
printf("%d ", bcc[i].size());
|
|
for (int j = 0; j < bcc[i].size(); j++)
|
|
printf("%d ", bcc[i][j]);
|
|
printf("\n");
|
|
}
|
|
return 0;
|
|
} |