-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourseSchedule.cpp
More file actions
47 lines (38 loc) · 858 Bytes
/
Copy pathcourseSchedule.cpp
File metadata and controls
47 lines (38 loc) · 858 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
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, x, y;
cin >> n >> m;
vector<int> g[n+1];
vector<int> inDegree(n+1, 0);
for(int i = 1; i <= m; i++) {
cin >> x >> y;
g[x].push_back(y);
inDegree[y] +=1;
}
queue<int> qu;
for(int i = 1; i <= n; i++) {
if(inDegree[i] == 0)
qu.push(i);
}
vector<int> ans;
while(!qu.empty()) {
x = qu.front();
qu.pop();
ans.push_back(x);
for(int i = 0; i < g[x].size(); i++) {
int y = g[x][i];
if(--inDegree[y] == 0)
qu.push(y);
}
}
if(ans.size() == n) {
for(int i = 0; i < n; i++)
cout << ans[i] << " ";
cout << endl;
}
else {
cout << "IMPOSSIBLE\n";
}
return 0;
}