-
Notifications
You must be signed in to change notification settings - Fork 0
/
topological_sort_LC207.cpp
81 lines (77 loc) · 2 KB
/
topological_sort_LC207.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
#include<iostream>
using namespace std;
#include<vector>
#include<stack>
#include<queue>
//...
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> visited;
//init
for (int i = 0; i < numCourses; i++) {
visited[i] = 0;
}
stack<int> res;//in fact not need
stack<int> stack;
int flag = 0;
int index = 0;
while (index < numCourses) {
if (visited[index] == 0) {
return DFS(index, stack, prerequisites,visited);
}
else {
continue;
}
}
}
private:
bool DFS(int index, stack<int> &stack, vector<pair<int, int>>& prerequisites,vector<int> visited) {
stack.push(index);
int cur = stack.top();
int flag = 0;
for (int i = 0; prerequisites.size(); i++) {
if (prerequisites[i].first == cur && visited[prerequisites[i].second] == 1) {
return false;
}
if (prerequisites[i].first == cur&&visited[prerequisites[i].second]==0) {
DFS(prerequisites[i].second, stack, prerequisites, visited);
flag = 1;
break;
}
else continue;
}
if (flag == 0) {
visited[cur] = 1;
}
}
};
/*
auto pointer!
net solution
*/
class Solution {
public:
//can finish as long as there is no circle
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int> > graph(numCourses, vector<int>(0)); //存储有向图
vector<int> visit(numCourses, 0); //记录访问状态
for (auto a : prerequisites) { //初始化有向图
graph[a.second].push_back(a.first);
}
for (int i = 0; i < numCourses; ++i) {
if (!canFinishDFS(graph, visit, i)) return false;
}
return true;
}
bool canFinishDFS(vector<vector<int> > &graph, vector<int> &visit, int i) {
if (visit[i] == -1) return false;//if we get -1, it means a circle
if (visit[i] == 1) return true;
visit[i] = -1;//stand for this time's search root!
for (auto a : graph[i]) {
if (!canFinishDFS(graph, visit, a)) return false;
}
visit[i] = 1;
return true;
}
};