-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse-schedule.cpp
More file actions
80 lines (63 loc) · 1.8 KB
/
course-schedule.cpp
File metadata and controls
80 lines (63 loc) · 1.8 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
/*
* Copyright (c) 2020 Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
#include <algorithm>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>> &prerequisites) {
// adjacency list of prereqs, slightly more efficient representation
// than edge list
vector<vector<int>> adj(numCourses);
vector<bool> queued(numCourses);
vector<bool> visited(numCourses, false);
for (auto &p : prerequisites) {
auto course = p[0];
auto req = p[1];
adj[course].push_back(req);
}
// for (auto &p : adj) {
// cout << "adj[" << p.first << "]: " << p.second << endl;
//}
// for each vertex
for (int v = 0; v < numCourses; ++v) {
// cout << "DFS starting with " << v << endl;
if (visited[v]) {
// cout << "skipping " << v << " as its already visited" << endl;
continue;
}
deque<int> to_visit;
fill(queued.begin(), queued.end(), false);
to_visit.push_front(v);
while (!to_visit.empty()) {
// recycle 'v'
v = to_visit.front();
if (visited[v]) {
queued[v] = false;
to_visit.pop_front();
} else {
// cout << "visited " << v << endl;
visited[v] = true;
queued[v] = true;
}
for (const auto &u : adj[v]) {
if (!visited[u]) {
to_visit.push_front(u);
} else if (queued[u]) {
// if an item is already visited and it is re-queued to visit,
// it clearly satisfies the criteria for introducing a cycle
// cout << "cycle detected at " << u << endl;
return false;
}
}
}
}
return true;
}
};