-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycle_detection.cpp
More file actions
45 lines (44 loc) · 876 Bytes
/
Copy pathCycle_detection.cpp
File metadata and controls
45 lines (44 loc) · 876 Bytes
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<vector<int>> g;
vector<int> color;
bool dfs(int s,int p) {
color[s]=1;
for (auto u: g[s]) {
if(u!=p){
if(color[u]==1){
return true;
}
else if(color[u]==0){
if(dfs(u,s)){
return true;
}
}
}
}
color[s]=2;
return false;
}
int main(){
int n,m;
cin>>n>>m;
g.resize(n);
color.assign(n,0);
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
u--,v--;
g[u].push_back(v);
}
for(int i=0;i<n;i++){
if(color[i]==0){
if(dfs(i,-1)){
cout<<"Cycle detected"<<endl;
return 0;
}
}
}
cout<<"No cycle detected"<<endl;
return 0;
}