-
Notifications
You must be signed in to change notification settings - Fork 4
/
cycle_dir_print.cpp
71 lines (59 loc) · 1.29 KB
/
cycle_dir_print.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
void print1d(const vector<int>& vec) {for (auto val : vec) {cout << val << " ";} cout << endl;}
void print2d(const vector<vector<int>>& vec) {for (auto row : vec) {for (auto val : row) {cout << val << " ";} cout << endl;}}
#define N 100
vvi adj;
vi color;
vi parent;
int cycle_start, cycle_end;
bool dfs(int v) {
color[v] = 1; // pending
for (auto u : adj[v]) {
if (color[u] == -1) {
// not visited
parent[u] = v;
if (dfs(u))
return true;
} else if (color[u] == 1) {
cycle_start = u;
cycle_end = v;
return true;
}
}
color[v] = 2;
return false;
}
void solve() {
int n, m;
cin >> n >> m;
color.assign(n + 1, -1);
parent.assign(n + 1, -1);
adj.assign(n + 1, {});
while (m--) {
int x, y;
cin >> x >> y;
adj[x].push_back(y);
}
cycle_start = -1;
for (int i = 1; i <= n; i++) {
if (color[i] == -1 and dfs(i))
break;
}
if (cycle_start == -1)
cout << "NO CYCLE in the graph" << endl;
else {
cout << "Cycle present in the graph" << endl;
// cout << cycle_start << " " << cycle_end << endl;
vi route;
route.push_back(cycle_start);
for (int i = cycle_end; i != cycle_start; i = parent[i]) {
route.push_back(i);
}
route.push_back(cycle_start);
reverse(route.begin(), route.end());
print1d(route);
}
}
signed main() {
solve();
return 0;
}