-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourseSchedule.cpp
48 lines (39 loc) · 1.13 KB
/
CourseSchedule.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
48
class Solution {
public:
vector<bool> Running;
vector<bool> Visited;
vector<vector<int>> Adj;
bool canFinish(int NumCourses, vector<vector<int>>& Pre) {
Adj = vector<vector<int>>(NumCourses);
Visited= vector<bool>(NumCourses, false);
Running = vector<bool>(NumCourses, false);
for(int i=0; i<Pre.size(); i++){
Adj[Pre[i][0]].push_back(Pre[i][1]);
}
for(int i = 0 ; i<NumCourses; i++){
if(!Visited[i]){
if(detect(i)){
return false;
}
}
}
return true;
}
bool detect(int i){
Visited[i] = true;
Running[i] = true;
for(int j: Adj[i]){
if(!Visited[j]){
if(detect(j)){
return true;
}
} else{
if(Running[j]){
return true;
}
}
}
Running[i] = false;
return false;
}
};