-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopo sort using kahn's algo.cpp
66 lines (61 loc) · 1.16 KB
/
topo sort using kahn's algo.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
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define int long long
#define all(x) x.begin(),x.end()
const int mod=1e9+7;
const int inf = 1e9;
const int N=1e4+9;
vector<int>adj[N];
int n, m;
vector<int>in(N);
vector<int>topo_sort;
bool kahn()
{
priority_queue<int, vector<int>, greater<int> > pq;
for(int i = 1; i <= n; i++)
{
if(in[i] == 0) pq.push(i);
}
while(!pq.empty())
{
int node = pq.top(); pq.pop();
topo_sort.pb(node);
for(auto c: adj[node])
{
in[c]--;
if(in[c] == 0) pq.push(c);
}
}
return topo_sort.size() == n;
}
void solve()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
in[v]++;
adj[u].pb(v);
}
if(!kahn())
{
cout << "Sandro fails.\n";
return;
}
for(auto c: topo_sort) cout << c << " "; cout << endl;
}
int32_t main()
{
IOS;
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}