-
Notifications
You must be signed in to change notification settings - Fork 0
/
course_schedule.cpp
40 lines (33 loc) · 1021 Bytes
/
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
class Solution {
public:
bool iscycle(vector<int> adj[],vector<int> &vis,int id){
if(vis[id]==1){
return true;
}
if(vis[id]==0){
vis[id]=1;
for(auto edge : adj[id]){
if(iscycle(adj,vis,edge)){
return true;
}
}
}
vis[id] = 2; //when returing to the parent calling function indicating that for this node there is no adjacent or all the child is already processed
return false;
}
bool canFinish(int n, vector<vector<int>>& pre) {
vector<int> adj[n];
for(auto edge : pre){
adj[edge[0]].push_back(edge[1]);
}
vector<int> vis(n,0);
for(int i=0;i<n;i++){
if(!vis[i]){
if(iscycle(adj, vis, i)){
return false;
}
}
}
return true;
}
};