-
Notifications
You must be signed in to change notification settings - Fork 0
/
207. Course Schedule.cpp
47 lines (39 loc) · 1.17 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
43
44
45
46
47
class Solution {
public:
bool DFSRec(int s, vector<bool>&visited, vector<bool>&currVisited, vector<int>adj[]){
visited[s] = true;
currVisited[s] = true;
vector<int>data = adj[s];
for(auto x: data){
if(!visited[x]){
if(DFSRec(x, visited, currVisited, adj)){
return true;
}
}
else if(visited[x] && currVisited[x]){
return true;
}
}
currVisited[s] = false;
return false;
}
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
int n = numCourses;
vector<bool>visited(n, false), currVisited(n, false);
vector<int>adj[n];
for(auto x: prerequisites){
vector<int>data = x;
int a = data[0];
int b = data[1];
adj[a].push_back(b);
}
for(int i=0; i<n; i++){
if(!visited[i]){
if(DFSRec(i, visited, currVisited, adj)){
return false;
}
}
}
return true;
}
};