-
Notifications
You must be signed in to change notification settings - Fork 0
/
200317-1.cpp
43 lines (41 loc) · 955 Bytes
/
200317-1.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
41
42
43
// https://leetcode-cn.com/problems/course-schedule/
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
unordered_map<int, unordered_set<int>> o;
for (auto& e : prerequisites) {
int a = e[0], b = e[1];
if (search(o, b, a)) return false;
o[a].insert(b);
}
return true;
}
private:
bool search(const unordered_map<int, unordered_set<int>>& o, int from, int to) {
if (from == to) return true;
auto it = o.find(from);
if (it == o.end()) return false;
for (auto& e : it->second) {
if (search(o, e, to)) return true;
}
return false;
}
};
int main()
{
Solution s;
{
vector<vector<int>> a = {{1,0}};
cout << s.canFinish(2, a) << endl; // answer: true
}
{
vector<vector<int>> a = {{1,0},{0,1}};
cout << s.canFinish(2, a) << endl; // answer: false
}
return 0;
}