-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathDetect_Cycle_Directed.cpp
107 lines (92 loc) · 1.95 KB
/
Detect_Cycle_Directed.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
104
105
106
107
/*
Detect Cycle in a Directed Graph
@Complexity - O(N+E)
@author Amirul islam
_
_|_ o._ o._ __|_| _. _|_
_>| ||| ||| |(_|| |(_|_>| |
_|
*/
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <list>
#include <iostream>
#include <assert.h>
#define mem(x,y) memset(x,y,sizeof(x))
#define CLEAR(x) memset(x,0,sizeof(x))
#define pb push_back
#define Sort(v) sort(v.begin(),v.end())
#define _sort(s, n) sort(s, s+n)
#define sqr(x) ((x)*(x))
#define le 1000009
#define ERR 1e-9
#define pi (2*acos(0))
#define PI 3.141592653589793
#define scanint(a) scanf("%d", &a)
#define scanLLD(a) scanf("%lld", &a)
typedef long long ll;
using namespace std;
/*--------------**---------------*/
vector <bool> vis;
vector <vector <int> > g;
vector <bool> recStack; // recursion stack
int nodes, edges;
bool isCycleUtil(int v) {
if (!vis[v]) {
vis[v] = true;
recStack[v] = true;
for (auto i = g[v].begin(); i != g[v].end(); i++) {
if (!vis[*i] && isCycleUtil(*i)) return true;
else if (recStack[*i]) return true;
}
}
recStack[v] = false;
return false;
}
bool isCyclic() {
vis.assign(nodes, false);
recStack.assign(nodes, false);
for (int i = 0; i < nodes; i++)
if (isCycleUtil(i))
return true;
return false;
}
int main() {
scanf("%d %d", &nodes, &edges);
g.assign(nodes, vector<int>());
int u, v;
for (int i = 0; i < edges; i++) {
scanf("%d %d", &u, &v);
g[u].pb(v);
}
if (isCyclic()) printf("Graph contains cycle\n");
else printf("Graph doesn't contain cycle\n");
return 0;
}
/*
Input:
----
// 1st line 4 nodes, 6 edges
4 6
0 1
0 2
1 2
2 0
2 3
3 3
Output:
-----
Graph contains cycle
*/