-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1679.cpp
More file actions
43 lines (40 loc) · 712 Bytes
/
Copy path1679.cpp
File metadata and controls
43 lines (40 loc) · 712 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
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, mark[N];
vector<int> g[N], order;
bool cycle;
void dfs(int u) {
mark[u] = 1;
for (int v: g[u]) {
if (!mark[v]) {
dfs(v);
} else if (mark[v] == 1) {
cycle = true;
return;
}
}
order.emplace_back(u);
mark[u] = 2;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1, u, v; i <= m; ++i) {
cin >> u >> v;
g[v].emplace_back(u);
}
for (int i = 1; i <= n; ++i) {
if (!mark[i]) {
dfs(i);
}
}
if (cycle) {
cout << "IMPOSSIBLE\n";
} else {
for (int u: order) {
cout << u << " ";
}
}
return 0;
}