-
Notifications
You must be signed in to change notification settings - Fork 110
/
207-course-schedule.cpp
42 lines (38 loc) · 1.35 KB
/
207-course-schedule.cpp
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
#include <unordered_map>
#include <unordered_set>
class Solution {
public:
bool dfs(int i, vector<bool>& visited, unordered_set<int>& visiting, unordered_map<int, vector<int>>& graph) {
if (visiting.find(i) != visiting.end()) return false;
visiting.emplace(i);
for (int n : graph[i]) {
if (!visited[i]) {
bool finishPossible = dfs(n, visited, visiting, graph);
if (!finishPossible) return false;
}
}
visited[i] = true;
visiting.erase(i);
return true;
}
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
unordered_map<int, vector<int>> graph;
for (const vector<int>& prereq : prerequisites) {
auto it = graph.find(prereq[0]);
if (it != graph.end()) {
(it->second).push_back(prereq[1]);
} else {
graph[prereq[0]] = {prereq[1]};
}
}
vector<bool> visited(numCourses, false);
unordered_set<int> visiting;
for (int i = 0; i < numCourses; i++) {
if (!visited[i]) {
bool finishPossible = dfs(i, visited, visiting, graph);
if (!finishPossible) return false;
}
}
return true;
}
};