-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinding-circle.cpp
More file actions
66 lines (57 loc) · 1.27 KB
/
finding-circle.cpp
File metadata and controls
66 lines (57 loc) · 1.27 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
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> g;
vector<int> color;
vector<int> parents;
int cycle_start, cycle_end = -1;
void dfs (int v) {
color[v] = 1;
for (auto i = g[v].begin(); i != g[v].end(); i++) {
if (color[*i] == 0) {
parents[*i] = v;
dfs(*i);
}
else if (color[*i] == 1) {
cycle_start = *i;
cycle_end = v;
}
}
color[v] = 2;
}
int main() {
freopen("cycle.in", "r", stdin);
freopen("cycle.out", "w", stdout);
int n, m;
cin >> n >> m;
g.resize(n);
color.resize(n, 0);
parents.resize(n, -1);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y);
}
for (int i = 0; i < n; i++) {
if (color[i] == 0) {
dfs(i);
}
}
if (cycle_end == -1) {
cout << "NO\n";
return 0;
}
else {
cout << "YES\n";
vector<int> ans;
for (int i = cycle_end; i != cycle_start; i = parents[i]) {
ans.push_back(i);
}
ans.push_back(cycle_start);
for (int i = (int) ans.size() - 1; i >= 0; --i) {
cout << ans[i] + 1 << " ";
}
}
return 0;
}