-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0207-course-schedule.cpp
More file actions
38 lines (35 loc) · 931 Bytes
/
Copy path0207-course-schedule.cpp
File metadata and controls
38 lines (35 loc) · 931 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
class Solution {
public:
bool canFinish(int n, vector<vector<int>>& pre) {
vector<vector<int>> g(n);
vector<int> indeg(n, 0);
for (auto &e: pre) {
g[e[1]].push_back(e[0]);
indeg[e[0]]++;
}
vector<int> done(n, false);
queue<int> q;
for (int i = 0; i < n; i++) {
if (indeg[i] == 0) {
q.push(i);
done[i] = true;
}
}
while (!q.empty()) {
int node = q.front(); q.pop();
for (auto &nei: g[node]) {
indeg[nei]--;
if (indeg[nei] == 0) {
q.push(nei);
done[nei] = true;
}
}
}
for (int i = 0; i < n; i++) {
if (!done[i]) {
return false;
}
}
return true;
}
};