-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcourse_schedule.cpp
More file actions
40 lines (37 loc) · 807 Bytes
/
course_schedule.cpp
File metadata and controls
40 lines (37 loc) · 807 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
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
int N, M, deg[100000];
vector<int> graph[100000];
int32_t main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> N >> M;
for(int i = 0; i < M; i++){
int a, b; cin >> a >> b;
graph[a-1].push_back(b-1);
deg[b-1]++;
}
vector<int> ans;
queue<int> q;
for(int i = 0; i < N; i++){
if(!deg[i]) q.push(i);
}
while(!q.empty()){
int id = q.front();
q.pop();
ans.push_back(id);
for(int i : graph[id]){
if(!--deg[i]){
q.push(i);
}
}
}
if((int)ans.size() < N){
cout << "IMPOSSIBLE" << endl;
}else{
for(int i : ans){
cout << i+1 << " ";
}
}
cout << endl;
}