forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1679 - Course Schedule.cpp
More file actions
57 lines (52 loc) · 1.02 KB
/
1679 - Course Schedule.cpp
File metadata and controls
57 lines (52 loc) · 1.02 KB
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
57
// Course Schedule
//
// Problem name: Course Schedule
// Problem Link: https://cses.fi/problemset/task/1679
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MAXN 200100
#define INF 1000000001
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
const int M = 1e9+7;
vector<int> v[MAXN], ans;
bool valid = true;
int cor[MAXN];
void dfs(int node) {
cor[node] = 1;
for (int x : v[node]) {
if (cor[x] == 1) {
valid = false;
return;
}
if (!cor[x]) dfs(x);
}
cor[node] = 2;
ans.pb(node);
}
int main () { _
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
v[a].pb(b);
}
for (int i = 1; i <= n && valid; i++) {
if (!cor[i]) {
dfs(i);
}
}
if (!valid) cout << "IMPOSSIBLE\n";
else {
reverse(ans.begin(), ans.end());
for (int x : ans) cout << x << ' ';
cout << '\n';
}
return 0;
}