forked from diptayan2k/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopological_Sort.cpp
More file actions
76 lines (52 loc) · 1.03 KB
/
Topological_Sort.cpp
File metadata and controls
76 lines (52 loc) · 1.03 KB
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
#include <iostream>
#include<bits/stdc++.h>
#define ll long long int
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define g(i,a,b) for(ll i=a;i>=b;i--)
#define F first
#define vv vector
#define S second
#define mp make_pair
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
using namespace std;
stack<ll> s;
vector<ll> v[100001];
bool vis[100001];
void topological_sort(ll u)
{
vis[u]=true;
//cout<<u<<" ";
if(!v[u].empty())
{
f(i,0,v[u].size()-1)
{
if(!vis[v[u][i]])
{
topological_sort(v[u][i]);
}
}
}
s.push(u);
}
int main()
{ memset(vis,false,sizeof(vis));
ll n,m;
cin>>n>>m;
f(i,0,m-1)
{ ll x,y;
cin>>x>>y;
v[x].pb(y);
}
f(i,1,n)
{
if(!vis[i]) topological_sort(i);
}
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
}