-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVDCC.cpp
More file actions
70 lines (70 loc) · 1.42 KB
/
VDCC.cpp
File metadata and controls
70 lines (70 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g, vdccs;
vector<int> dfn, low;
vector<bool> cut;
stack<int> stk;
int tot, rt;
// 求点双连通分量(vDCC)
void tarjan(int u) {
dfn[u] = low[u] = tot++;
stk.push(u);
// 特判一个点的vDCC
if (u == rt && g[u].empty()) {
vdccs.push_back({u});
return;
}
int son = 0;
for (auto v : g[u]) {
if (dfn[v] == -1) {
tarjan(v);
low[u] = min(low[u], low[v]);
// 找到vDCC
if (dfn[u] > low[v]) continue;
son++;
// 找到割点
if (u != rt || son > 1) cut[u] = true;
vdccs.push_back({});
int w;
do {
w = stk.top();
stk.pop();
vdccs.back().push_back(w);
} while (w != v);
vdccs.back().push_back(u);
} else {
low[u] = min(low[u], dfn[v]);
}
}
}
/**
* @brief 使用示例
*
* @note 测试链接: https://www.luogu.com.cn/problem/P8435
*/
int main() {
int n, m;
cin >> n >> m;
g.resize(n);
dfn.resize(n, -1);
low.resize(n, -1);
cut.resize(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
u--;
v--;
if (u == v) continue;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < n; ++i)
if (dfn[i] == -1) tarjan(rt = i);
cout << vdccs.size() << "\n";
for (auto& vdcc : vdccs) {
cout << vdcc.size() << " ";
for (auto u : vdcc) cout << u + 1 << " ";
cout << "\n";
}
return 0;
}