-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.cpp
More file actions
48 lines (42 loc) · 904 Bytes
/
tempCodeRunnerFile.cpp
File metadata and controls
48 lines (42 loc) · 904 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;
void topologicalSortUtils(vector<int>graph[],bool visited[],stack<int>&st,int i)
{
visited[i] = true;
int n = graph[i].size();
for(int j=0;j<n;++j)
{
if(!visited[graph[i][j]])
topologicalSortUtils(graph,visited,st,graph[i][j]);
}
st.push(i);
}
void topologicalSort(vector<int>graph[],int V)
{
bool visited[V];
stack<int>st;
for(int i=0;i<V;++i)
visited[i]=false;
for(int i=0;i<V;++i)
{
if(!visited[i])
topologicalSortUtils(graph,visited,st,i);
}
while(!st.empty())
{
cout<<st.top()<<" "<<endl;
st.pop();
}
}
int main()
{
int V,E,s,d;
cin>>V>>E;
vector<int>graph[V];
for(int i=0;i<E;++i)
{
cin>>s>>d;
graph[s].push_back(d);
}
topologicalSort(graph,V);
}