forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
103 lines (81 loc) · 2.42 KB
/
main3.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
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
92
93
94
95
96
97
98
99
100
101
102
103
/// Source : https://leetcode.com/problems/course-schedule-ii/
/// Author : liuyubobobo
/// Time : 2018-12-16
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
/// Topological Order based on DFS
/// Time Complexity: O(V + E)
/// Space Complexity: O(V + E)
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int>> g(numCourses);
for(const pair<int, int>& p: prerequisites){
int from = p.second;
int to = p.first;
g[from].push_back(to);
}
if(hasCircle(g)) return {};
return topoOrder(g);
}
private:
bool hasCircle(const vector<vector<int>>& g){
vector<bool> visited(g.size(), false);
vector<bool> onPath(g.size(), false);
for(int i = 0; i < g.size(); i ++)
if(!visited[i])
if(circleDFS(g, i, visited, onPath))
return true;
return false;
}
bool circleDFS(const vector<vector<int>>& g, int v,
vector<bool>& visited, vector<bool>& onPath){
visited[v] = true;
onPath[v] = true;
for(int next: g[v])
if(!visited[next]){
if(circleDFS(g, next, visited, onPath))
return true;
}
else if(onPath[next])
return true;
onPath[v] = false;
return false;
}
vector<int> topoOrder(const vector<vector<int>>& g){
vector<bool> visited(g.size(), false);
stack<int> st;
for(int i = 0; i < g.size(); i ++)
if(!visited[i])
topoDFS(g, i, visited, st);
vector<int> res;
while(!st.empty()){
res.push_back(st.top());
st.pop();
}
return res;
}
void topoDFS(const vector<vector<int>>& g, int v, vector<bool>& visited, stack<int>& st){
visited[v] = true;
for(int next: g[v])
if(!visited[next])
topoDFS(g, next, visited, st);
st.push(v);
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
vector<pair<int, int>> pre1 = {{1,0}};
print_vec(Solution().findOrder(2, pre1));
// 0 1
vector<pair<int, int>> pre2 = {{1,0},{2,0},{3,1},{3,2}};
print_vec(Solution().findOrder(4, pre2));
// 0 1 2 3
return 0;
}