-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathG_Course_Schedule.cpp
More file actions
82 lines (67 loc) · 1.36 KB
/
G_Course_Schedule.cpp
File metadata and controls
82 lines (67 loc) · 1.36 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <numeric>
#define int long long
using namespace std;
int n , m;
vector<vector<int>> adj;
vector<int> indegree;
void solve() {
cin >> n >> m;
adj.assign(n+1 , vector<int>());
indegree.assign(n+1 , 0);
for(int i=0 ; i<m ; i++){
int u , v;
cin >> u >> v;
adj[u].push_back(v);
indegree[v]++;
}
queue<int> q;
for(int i=1 ; i<=n ; i++){
if(indegree[i] == 0) q.push(i);
}
vector<int> ans;
while(!q.empty()){
int curr = q.front();
ans.push_back(curr);
q.pop();
for(int nb : adj[curr]){
indegree[nb]--;
if(indegree[nb] == 0){
q.push(nb);
}
}
}
if(ans.size() != n) cout << "IMPOSSIBLE";
else {
for(int i=0 ; i<n ; i++){
cout << ans[i] << " ";
}
}
cout << endl;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int _t = 1;
// cin >> _t;
while(_t--){
solve();
}
return 0;
}