-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy path207_Course_Schedule
More file actions
30 lines (28 loc) · 873 Bytes
/
207_Course_Schedule
File metadata and controls
30 lines (28 loc) · 873 Bytes
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
Leetcode 207 : Course Schedule
==========================================================
C++:
----
class Solution {
bool dfs(int s, vector<vector<int>>& adj, vector<int>& visited){
visited[s] = 1;
for(auto u: adj[s]){
if(visited[u] == 1) return true;
else if(visited[u] == 0){
if(dfs(u, adj, visited)) return true;
}
}
visited[s] = 2;
return false;
}
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> adj(numCourses, vector<int>());
for(auto edge: prerequisites)
adj[edge[1]].push_back(edge[0]);
vector<int> visited(numCourses, 0);
for(int i = 0; i < numCourses; ++i){
if(!visited[i] && dfs(i, adj, visited)) return false;
}
return true;
}
};