-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path207-CourseSchedule.cpp
47 lines (39 loc) · 1.22 KB
/
207-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
class Solution {
public:
unordered_map<int, int> visit;
unordered_map<int, vector<int>> g;
bool cycleDetect(int node){
vector<int> ::iterator it;
visit[node] = 1;
bool answer = true;
cout << "Visiting " << node << endl;
for(it = g[node].begin(); it != g[node].end(); it++){
cout << "Child " << *it << " . " << visit[*it] << endl;
if(visit[*it] == 1) answer = false;
else if(visit[*it] == 0){
if(cycleDetect(*it) == false){
answer = false;
}
}
}
visit[node] = 2;
return answer;
}
bool canFinish(int numCourses, vector<pair<int, int> >& prq) {
int len = prq.size();
if(len <= 1) return true;
vector<pair<int, int>> ::iterator it;
for(it = prq.begin(); it != prq.end(); it++){
g[it->second].push_back(it->first);
visit[it->second] = 0;
visit[it->first] = 0;
}
bool flag = false;
for(int i = 0; i < numCourses; i++){
if(visit[i] == 0){
if(cycleDetect(i) == false) return false;
}
}
return true;
}
};