-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtopological_sort.cpp
More file actions
48 lines (36 loc) · 814 Bytes
/
Copy pathtopological_sort.cpp
File metadata and controls
48 lines (36 loc) · 814 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int n, m;
int status[maxn] = {};
vector<int>adj[maxn];
vector<int>vec;
bool has_cycle = false;
void dfs(int a){
if (status[a] == 1) return void(has_cycle = true);
status[a] = 1;
for(int b:adj[a]){
if (status[b] == 2) continue;
dfs(b);
}
vec.push_back(a);
status[a] = 2;
}
void topological_sort(){
for(int i = 1;i <= n;i++){
if (status[i] == 2) continue;
dfs(i);
}
reverse(vec.begin(), vec.end());
}
int main(){
cin>>n>>m;
for(int i = 0;i < m;i++){
int a, b;cin>>a>>b;
adj[a].push_back(b);
}
topological_sort();
if (has_cycle) cout << "the given graph has cycle." << endl;
else for(int e:vec) cout << e << " ";
return 0;
}