-
Notifications
You must be signed in to change notification settings - Fork 0
/
207. Course Schedule.cpp
42 lines (34 loc) · 965 Bytes
/
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
class Solution {
public:
bool dfs(int cur, vector<int>&vis, unordered_map<int, vector<int>>& dep){
vis[cur] = 1;
for(auto n: dep[cur]){
if(vis[n] == 1){
return false;
}
if (vis[n] == 0){
bool ret = dfs(n, vis, dep);
if (ret == false) {
return false;
}
}
}
vis[cur] = 2;
return true;
}
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
unordered_map<int, vector<int> > dep;
for(auto pre: prerequisites){
dep[pre[0]].push_back(pre[1]);
}
vector<int> vis(numCourses+1, 0);
for(int i = 1; i <= numCourses; i++){
if(vis[i] == 0){
bool ret = dfs(i, vis, dep);
if(ret == false)
return false;
}
}
return true;
}
};