-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsq4-E.cpp
More file actions
48 lines (43 loc) · 674 Bytes
/
Copy pathsq4-E.cpp
File metadata and controls
48 lines (43 loc) · 674 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
/* Topological Sort */
#include <bits/stdc++.h>
using namespace std;
int n;
list<int> *graph;
stack<int> s;
bool *vis;
void tSort(int);
int main()
{
freopen("in", "r", stdin);
freopen("out", "w", stdout);
int e;
cin>>n>>e;
graph = new list<int>[n];
vis = new bool[n];
for(int i=0; i<n; i++) vis[i] = 0;
for(int i=0; i<e; i++)
{
int u, v;
cin>>u>>v;
graph[u].push_back(v);
}
for(int i=0; i<n; i++)
{
if(!vis[i]) tSort(i);
}
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
void tSort(int u)
{
vis[u] = 1;
for(list<int>::iterator it=graph[u].begin(); it!=graph[u].end(); it++)
{
if(!vis[*it]) tSort(*it);
}
s.push(u);
}