-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopological_Sort.cpp
More file actions
44 lines (37 loc) · 910 Bytes
/
Topological_Sort.cpp
File metadata and controls
44 lines (37 loc) · 910 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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool visited[100];
vector<ll>graph[10000];
vector<ll>ans;
void dfs(int source) {
visited[source] = true;
for (int i = 0; i < graph[source].size(); i++) {
ll child = graph[source][i];
if (!visited[child]) {
dfs(child);
}
}
ans.push_back(source);
}
void solve() {
ll node , edge;
cin >> node >> edge;
for (int i = 0; i < edge; i++) {
ll u, v;
cin >> u >> v;
graph[u].push_back(v);
}
for (int i = 1; i <= node; i++) {
if (!visited[i]) {
dfs(i);
}
}
reverse(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
}
int main() {
solve();
}