-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcourse-schedule.cpp
34 lines (33 loc) · 994 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
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>> &prerequisites)
{
unordered_map<int, vector<int>> graph;
for (int i = 0; i < prerequisites.size(); i++)
{
graph[prerequisites[i][0]].push_back(prerequisites[i][1]);
}
unordered_set<int> visited;
for (int course = 0; course < numCourses; course++)
{
if (!dfs(course, graph, visited))
{
return false;
}
}
return true;
}
bool dfs(int course, unordered_map<int, vector<int>> &graph, unordered_set<int> &visited)
{
if (visited.find(course) != visited.end()) return false;
if (graph[course].empty()) return true;
visited.insert(course);
for (int x: graph[course])
{
if (!dfs(x, graph, visited)) return false;
}
graph[course].clear();
visited.erase(course);
return true;
}
};