forked from nishitpanchal395/projecthactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KahnAlgo.cpp
51 lines (51 loc) · 849 Bytes
/
KahnAlgo.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
/*
Kahn Algorithm to find Topological Sort in Directed Acyclic Graph
IF the graph is not Aycyclic prints IMPOSSIBLE
*/
#include<bits/stdc++.h>
using namespace std;
vector<int> adj[100010];
void solve()
{
int n,m;
cin>>n>>m;
vector<int> in(n+1);
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
adj[x].push_back(y);
in[y]++;
}
stack<int> s;
for(int i=0;i<n;i++)
{
if(in[i+1]==0)
s.push(i+1);
}
vector<int> ans;
while(!s.empty())
{
int no=s.top();
s.pop();
ans.push_back(no);
for(auto x:adj[no])
{
in[x]--;
if(in[x]==0)
s.push(x);
}
}
if(ans.size()!=n)
cout<<"IMPOSSIBLE";
else
{
for(auto x:ans)
cout<<x<<" ";
}
}
int main()
{
solve();
return 0;
}