1
+ #include < iostream>
2
+ #include < vector>
3
+ #include < map>
4
+ using namespace std ;
5
+
6
+ // 拓扑排序方法判断图是否有环
7
+ class Solution1 {
8
+ public:
9
+ // (u,v)表示从u->v的边
10
+ bool canFinish (int numCourses, vector<vector<int > >& prerequisites) {
11
+ int count[numCourses];
12
+ vector<int > points;
13
+ // edge[i]存放所有从i出发与i相连的顶点
14
+ std::map<int , vector<int > > edge;
15
+ memset (count, 0 , sizeof (count));
16
+ for (int i = 0 ; i < prerequisites.size (); i++) {
17
+ vector<int > pair = prerequisites[i];
18
+ int u = pair[0 ];
19
+ int v = pair[1 ];
20
+ count[v]++;
21
+ if (edge.find (u) == edge.end ()) {
22
+ edge[u] = vector<int >();
23
+ }
24
+ edge[u].push_back (v);
25
+ }
26
+
27
+ for (int i = 0 ; i < numCourses; i++) {
28
+ if (count[i] == 0 ) {
29
+ points.push_back (i);
30
+ }
31
+ }
32
+
33
+ while (points.size ()) {
34
+ int u = points.back ();
35
+ points.pop_back ();
36
+ for (int v : edge[u]) {
37
+ count[v]--;
38
+ if (count[v] == 0 ) {
39
+ points.push_back (v);
40
+ }
41
+ }
42
+ }
43
+
44
+ for (int i = 0 ; i < numCourses; i++) {
45
+ if (count[i] != 0 ) {
46
+ return false ;
47
+ }
48
+ }
49
+ return true ;
50
+ }
51
+ };
52
+
53
+ // DFS判断图是否有环
54
+ class Solution {
55
+ public:
56
+ bool canFinish (int numCourses, vector<vector<int > >& prerequisites) {
57
+ // edge[i]临接表形式存放所有从i出发与i相连的顶点
58
+ std::map<int , vector<int > > edge;
59
+
60
+ for (int i = 0 ; i < prerequisites.size (); i++) {
61
+ vector<int > pair = prerequisites[i];
62
+ int u = pair[0 ];
63
+ int v = pair[1 ];
64
+ if (edge.find (u) == edge.end ()) {
65
+ edge[u] = vector<int >();
66
+ }
67
+ edge[u].push_back (v);
68
+ }
69
+ bool visit[numCourses];
70
+ bool handled[numCourses];
71
+ memset (visit, 0 , sizeof (visit));
72
+ memset (handled, 0 , sizeof (handled));
73
+ bool hasLoop = false ;
74
+
75
+ // 对每一个入度为0的点进行dfs,判断是否有环, handled表示之前dfs处理过的点不再处理
76
+ for (int i = 0 ; i < numCourses; i++) {
77
+ if (!handled[i]) {
78
+ hasLoop |= dfs (i, edge, visit, handled);
79
+ if (hasLoop) {
80
+ break ;
81
+ }
82
+ }
83
+ }
84
+
85
+ return !hasLoop;
86
+ }
87
+ // 对图进行深度遍历,如果一条链中有环,就会出现遍历的点已经遍历过
88
+ bool dfs (int start, const std::map<int , vector<int > > &edge, bool visit[], bool handled[]) {
89
+ if (visit[start]) {
90
+ return true ;
91
+ }
92
+ if (edge.find (start) == edge.end ())
93
+ return false ;
94
+
95
+ visit[start] = true ;
96
+ handled[start] = true ;
97
+ bool hasLoop = false ;
98
+ for (int i = 0 ; i < edge.at (start).size (); i++) {
99
+ hasLoop |= dfs (edge.at (start)[i], edge, visit, handled);
100
+ if (hasLoop) {
101
+ break ;
102
+ }
103
+ }
104
+ visit[start] = false ;
105
+ return hasLoop;
106
+ }
107
+ };
108
+ int main (int argc, char *argv[]) {
109
+ Solution s;
110
+ vector<vector<int > > v;
111
+ vector<int > v1;
112
+ v1.push_back (1 );
113
+ v1.push_back (0 );
114
+ vector<int > v2;
115
+ v2.push_back (0 );
116
+ v2.push_back (1 );
117
+ v.push_back (v1);
118
+ v.push_back (v2);
119
+ s.canFinish (2 , v);
120
+ }
0 commit comments