-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path207.cpp
More file actions
executable file
·33 lines (32 loc) · 1.09 KB
/
207.cpp
File metadata and controls
executable file
·33 lines (32 loc) · 1.09 KB
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
class Solution {
public:
unordered_map<int, vector<int>> graph_map;
bool flag[100005];
bool ans = false;
void dfs(int cur){
if (graph_map.count(cur) == 0) return;
if (flag[cur] == true) {ans = false; return;}
flag[cur] = true;
for (int i = 0; i < graph_map[cur].size(); i++)
dfs(graph_map[cur][i]);
flag[cur] = false;
if (ans == false) return;
}
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
int maxn = INT_MIN;
ans = true;
for (int i = 0; i < prerequisites.size(); i++){
if (graph_map.count(prerequisites[i][0]) == 0){
vector<int> tmp; tmp.push_back(prerequisites[i][1]);
graph_map[prerequisites[i][0]] = tmp;
}else
graph_map[prerequisites[i][0]].push_back(prerequisites[i][1]);
if (prerequisites[i][0] > maxn) maxn = prerequisites[i][0];
}
for (auto &v : graph_map){
dfs(v.first);
if (ans == false) return false;
}
return true;
}
};