-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscc.cpp
More file actions
87 lines (78 loc) · 1.71 KB
/
Copy pathscc.cpp
File metadata and controls
87 lines (78 loc) · 1.71 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef long double ld;
using namespace std;
template<int N> struct scc {
vector<int> g[N]; // directed graph
vector<int> cond[N]; // condensation of g
vector<int> rev[N]; // transpose of g
vector<int> L; // SCCs grouped topologically
int mapping[N]; // node i in g corresponds to mapping[i] in h
int visited[N];
int n;
int comps = 0;
scc() {}
scc(int _n) {n = _n;}
/* directed edge from u to v in g */
void addEdge(int u, int v) {
g[u].pb(v);
rev[v].pb(u);
}
void visit(int u) {
if (!visited[u]) {
visited[u] = true;
for (int v : g[u]) visit(v);
L.pb(u);
}
}
void assign(int v, int root) {
mapping[v] = root;
for (int u : rev[v]) {
if (mapping[u] == 0)
assign(u, root);
}
}
void computeSCC() {
for (int i = 1; i <= n; i++) visit(i);
reverse(L.begin(), L.end());
for (int x : L) cout << x << endl;
for (int u : L) {
if (mapping[u] == 0) assign(u, ++comps);
}
for (int i = 1; i <= n; i++) {
for (int j : g[i]) {
if (mapping[i] == mapping[j]) continue;
cond[mapping[i]].pb(mapping[j]);
}
}
for (int i = 1; i <= n; i++) {
sort(cond[i].begin(), cond[i].end());
cond[i].resize(unique(cond[i].begin(), cond[i].end()) - cond[i].begin());
}
}
};
const int N = 2e5 + 10;
scc<N> graph;
int main() {
int n; cin >> n;
graph.n = n;
for (int i = 1; i <= n; i++) {
while (true) {
int x; cin >> x; ++x;
if (x == 0) break;
graph.addEdge(i, x);
}
}
graph.computeSCC();
cout << graph.comps << endl;
for (int i = 1; i <= graph.comps; i++) {
for (int y : graph.cond[i]) {
cout << y - 1 << " ";
}
cout << -1 << endl;
}
return 0;
}