-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopologialSort.cpp
61 lines (50 loc) · 956 Bytes
/
TopologialSort.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
#include<iostream>
#include<algorithm>
#include<vector>
#include<sstream>
using namespace std;
#define WH 0
#define GR 1
#define BL 2
#define pb push_back
vector<int> color;
int n, m;
vector<vector<int> > g;
vector<int> top;
void dfs(int i)
{
color[i] = GR;
for(int j = 0; j < g[i].size(); j++){
if(color[g[i][j]] == WH){
dfs(g[i][j]);
}
else if(color[g[i][j]] == GR) {
cout << "NO" << endl;
exit(0);
}
}
color[i] = BL;
top.push_back(i);
}
int main()
{
cin >> n >> m;
g.resize(n + 1);
color.resize(n + 1);
for(int i = 0; i < m; i++){
int x, y;
cin >> x >> y;
g[x].pb(y);
}
for(int i = 0; i < n; i++){
if(color[i] == WH){
dfs(i);
}
}
cout << "YES" << endl;
for(int i = top.size() - 1; i >= 0; i--){
cout << top[i] << ' ';
}
cout << endl;
return 0;
}