-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathretele.cpp
89 lines (74 loc) · 1.83 KB
/
retele.cpp
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
88
89
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream in("retele.in");
ofstream out("retele.out");
vector<vector<int>> edges;
vector<bool> visited,on_stack;
vector<int> index, lowlink;
vector<vector<int>> components;
stack<int> nodes_visited;
int c_index = 0;
void strongconnect(int v) {
visited[v] = true;
on_stack[v] = true;
nodes_visited.push(v);
index[v] = c_index;
lowlink[v] = c_index++;
for(int w:edges[v]) {
if(!visited[w]) {
strongconnect(w);
lowlink[v] = min(lowlink[v],lowlink[w]);
}
else if(on_stack[w]) {
lowlink[v] = min(lowlink[v],index[w]);
}
}
if(index[v] == lowlink[v]) {
components.resize(components.size()+1);
int w;
do {
w = nodes_visited.top();
nodes_visited.pop();
on_stack[w] = false;
components.back().push_back(w);
} while(w != v);
}
}
bool sort_lexy(const vector<int> &c1, const vector<int> &c2) {
return c1[0] < c2[0];
}
int main() {
unsigned n,m;
in >> n >> m;
edges.resize(n+1);
visited.resize(n+1);
on_stack.resize(n+1);
index.resize(n+1);
lowlink.resize(n+1);
for(int i = 0; i < m; ++i) {
int a,b;
in >> a >> b;
edges[a].push_back(b);
}
for(int i = 1; i <= n; ++i) {
if(!visited[i])
strongconnect(i);
}
for(vector<int> & comp:components) {
sort(comp.begin(), comp.end());
}
sort(components.begin(), components.end(),sort_lexy);
out << components.size() << '\n';
for(auto comp:components) {
out << comp.size() << ' ';
for(int node:comp) {
out << node << ' ';
}
out << '\n';
}
return 0;
}