-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathD.cpp
86 lines (76 loc) · 1.61 KB
/
D.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string fileName = "cycle";
ofstream out(fileName + ".out");
ifstream in(fileName + ".in");
int n, m;
unsigned int MAX_N = 100 * 1000;
vector<vector<int>> edges(MAX_N);
vector<int> color(MAX_N, 0);
vector<bool> was(MAX_N, false);
vector<int> ans;
vector<int> path(MAX_N, -1);
int start, finish;
void input() {
in >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y;
in >> x >> y;
edges[x - 1].push_back(y - 1);
}
}
void output() {
out << "YES" << "\n";
vector<int> cycle;
for (int v = finish; v != start; v = path[v])
cycle.push_back(v + 1);
cycle.push_back(start + 1);
reverse(cycle.begin(), cycle.end());
for(auto i:cycle) out << i << " ";
}
void dfs(int v) {
was[v] = true;
for (auto to: edges[v]) {
if (!was[to])
dfs(to);
}
ans.push_back(v);
}
void solve() {
for (int i = 0; i < n; ++i)
if (!was[i])
dfs(i);
output();
}
bool cdfs(int v) {
color[v] = 1;
bool b = true;
for (auto i: edges[v]) {
if (color[i] == 0) {
path[i] = v;
if (!cdfs(i)) return false;
} else if (color[i] == 1) {
start = i;
finish = v;
return false;
}
}
color[v] = 2;
return true;
}
bool check() {
for (int i = 0; i < n; ++i)
if (color[i] == 0)
if (!cdfs(i)) return false;
return true;
}
int main() {
input();
if (!check()) output();
else out << "NO";
return 0;
}