0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

P8436 【模板】边双连通分量

https://www.luogu.com.cn/record/94754317
This commit is contained in:
Baoshuo Ren 2022-11-20 11:28:16 +08:00
parent 7b3f37a540
commit e2eb0106aa
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

86
Luogu/P8436/P8436.cpp Normal file
View File

@ -0,0 +1,86 @@
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 5e5 + 5,
M = 2e6 + 5;
int n, m, ans;
int idx, head[N], edge[M << 1], next[M << 1];
int cnt, dcc_cnt, dfn[N], low[N];
std::stack<int> st;
std::vector<int> nodes[N];
void add(int u, int v) {
next[idx] = head[u];
edge[idx] = v;
head[u] = idx++;
}
void tarjan(int u, int in_edge) {
dfn[u] = low[u] = ++cnt;
st.push(u);
for (int i = head[u]; ~i; i = next[i]) {
int v = edge[i];
if (!dfn[v]) {
tarjan(v, i);
low[u] = std::min(low[u], low[v]);
} else if (i != (in_edge ^ 1)) {
low[u] = std::min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u]) {
dcc_cnt++;
int v;
do {
v = st.top();
st.pop();
nodes[dcc_cnt].push_back(v);
} while (v != u);
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
std::fill_n(head, N, -1);
cin >> n >> m;
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v;
add(u, v);
add(v, u);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
tarjan(i, -1);
}
}
cout << dcc_cnt << endl;
for (int i = 1; i <= dcc_cnt; i++) {
cout << nodes[i].size() << ' ';
for (int x : nodes[i]) {
cout << x << ' ';
}
cout << endl;
}
return 0;
}