-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_schedule.cpp
More file actions
91 lines (82 loc) · 2.11 KB
/
course_schedule.cpp
File metadata and controls
91 lines (82 loc) · 2.11 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* =====================================================================================
*
* Filename: course_schedule.cpp
*
* Description: 207. Course Schedule. https://leetcode.com/problems/course-schedule/
*
* Version: 1.0
* Created: 04/06/23 15:24:29
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <tuple>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace std;
class Graph {
public:
Graph(const int n) : nodes_(n), edges_(n) {}
void addEdge(const int from, const int to) { edges_[from].push_back(to); }
bool isAcyclic() const {
vector<bool> visiting(nodes_, false);
vector<bool> visited(nodes_, false);
for (int i = 0; i < nodes_; i++) {
if (visited[i]) {
continue;
}
if (dfsCyclic(i, visiting, visited)) {
return false;
}
}
return true;
}
private:
// Traverse and check cycle by dfs
bool dfsCyclic(const int node, vector<bool>& visiting, vector<bool>& visited) const {
if (visiting[node]) {
return true;
}
if (visited[node]) {
return false;
}
visiting[node] = true;
visited[node] = true;
for (const auto& adj : edges_[node]) {
if (dfsCyclic(adj, visiting, visited)) {
return true;
}
}
visiting[node] = false;
return false;
}
private:
vector<vector<int>> edges_;
int nodes_;
};
// Depth first search in topological sorting
class Solution {
public:
bool canFinish(int num, vector<vector<int>>& prerequisites) {
Graph g(num);
for (const auto& p : prerequisites) {
g.addEdge(p[1], p[0]);
}
return g.isAcyclic();
}
};
TEST(Solution, canFinish) {
vector<tuple<int, vector<vector<int>>, bool>> cases = {
make_tuple(2, vector<vector<int>>{{1, 0}}, true),
make_tuple(2, vector<vector<int>>{{1, 0}, {0, 1}}, false),
};
for (auto& [n, p, finish] : cases) {
EXPECT_EQ(Solution().canFinish(n, p), finish);
}
}