-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopo.cpp
More file actions
45 lines (39 loc) · 809 Bytes
/
topo.cpp
File metadata and controls
45 lines (39 loc) · 809 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
#include<bits/stdc++.h>
using namespace std;
vector <int> adj[10];
bool visited[10];
stack<int>st;
int nodes, edges, x, y, connectedComponents = 0;
void dfs(int s) {
visited[s] = true;
for(int i = 0;i < adj[s].size();++i){
if(visited[adj[s][i]] == false)
dfs(adj[s][i]);
}
st.push(s);
}
void dfs_util(){
for(int i = 1;i <= nodes;++i) {
if(visited[i] == false) {
dfs(i);
}
}
}
void initialize() {
for(int i = 0;i < 10;++i)
visited[i] = false;
}
int main() {
cin >> nodes;
cin >> edges;
for(int i = 0;i < edges;++i){
cin >> x >> y;
adj[x].push_back(y);
}
initialize();
dfs_util();
while(!st.empty()){
cout << st.top() << "\t";
st.pop();
}
}