-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
34 lines (33 loc) · 962 Bytes
/
code.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
class Solution {
public:
bool rec(unordered_map<int,vector<int>> &umap,vector<int> &visited,int index){
if (visited[index]==1){
return false;
}
visited[index] = 1 ;
for( auto v : umap[index]){
if (visited[v]!=2){
if (!rec(umap,visited,v)){
return false;
}
}
}
visited[index] = 2 ;
return true;
}
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
unordered_map<int,vector<int>> umap ;
for(auto v : prerequisites){
umap[v[1]].push_back(v[0]) ;
}
vector<int> visited(numCourses,0);
for(int i = 0 ; i < numCourses; i++){
if(visited[i] != 2 ){
if (!rec(umap,visited,i)){
return false;
}
}
}
return true;
}
};