-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
30 lines (25 loc) · 912 Bytes
/
main.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
class Solution {
enum class State { processed, processing, unprocessed };
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> adj(numCourses);
for (auto& prerequisite : prerequisites) {
adj[prerequisite[0]].push_back(prerequisite[1]);
}
vector<State> states(numCourses, State::unprocessed);
function<bool(int)> dfs = [&](int u) -> bool {
if (states[u] == State::processing) return false;
if (states[u] == State::processed) return true;
states[u] = State::processing;
for (int v : adj[u]) {
if (!dfs(v)) return false;
}
states[u] = State::processed;
return true;
};
for (int i = 0; i < numCourses; ++i) {
if (!dfs(i)) return false;
}
return true;
}
};