-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopo.cpp
More file actions
56 lines (48 loc) · 947 Bytes
/
topo.cpp
File metadata and controls
56 lines (48 loc) · 947 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
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define RFOR(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
int n, m;
vector<int> adj[1000005];
vector<bool> visited;
vector<int> topo;
void nhap() {
cin >> m;
visited.resize(n+1, false);
for(int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
}
}
void dfs(int u) {
visited[u] = true;
for(int x: adj[u]) {
if(!visited[x]) dfs(x);
}
topo.push_back(u);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
nhap();
for(int i = 1; i <= n; i++) {
if(!visited[i]) dfs(i);
}
reverse(topo.begin(), topo.end());
for(int x:topo) cout << x << " " ;
}
// 7 7
// 1 2
// 2 3
// 2 4
// 3 5
// 4 5
// 1 6
// 7 6