-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCycleDFS.cpp
More file actions
54 lines (46 loc) · 989 Bytes
/
Copy pathCycleDFS.cpp
File metadata and controls
54 lines (46 loc) · 989 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
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
bool check_cycle(int node,int parent,map<int,set<int>> adj,map<int,bool> visit){
visit[node]=1;
for(int it: adj[node]){
if(!visit[it]){
if(check_cycle(it,node,adj,visit)){
return true;
}
}
else if(visit[it]==true && parent!=it){
return true;
}
}
return false;
}
void DFS(int v, map<int,set<int>> adj){
map<int,bool> visit;
int flag=0;
for(int i=0;i<v;i++){
if(!visit[i]){
if(check_cycle(i,-1,adj,visit)){
cout<<"true";
flag=1;
break;
}
}
}
if(flag==0){
cout<<"false";
}
}
int main(){
int n,m;
cin>>n>>m;
map<int, set<int>> adj;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
adj[u].insert(v);
adj[v].insert(u);
}
DFS(n, adj);
return 0;
}