-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycleinug.cpp
90 lines (87 loc) · 1.9 KB
/
cycleinug.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
#include <iostream>
#include <cmath>
#include <stack>
#include <cstring>
#include <stdlib.h>
#include <unordered_set>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <algorithm>
#include <queue>
using namespace std;
#define vi vector<int>
#define vvi vector<vi>
#define pii vector<int, int>
#define vii vector<pii>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define ff first
#define ss second
const int N=1e5+2;
/*
using dfs if back edge found its cyclic
*/
bool detect(int src, vector<int> adj[], int vis[]){
vis[src]=1;
queue<pair<int,int>> q;
q.push({src,-1});
while(!q.empty()){
int node=q.front().first;
int parent=q.front().second;
q.pop();
for(auto i:adj[node]){
if(!vis[i]){
vis[i]=1;
q.push({i,node});
}
else if(parent!=i){
return true;
}
}
}
return false;
}
bool iscycle(int v, vector<int> adj[]){
int vis[v]={0};
for(int i=0;i<v;i++){
if(!vis[i]){
if(detect(i,adj,vis)){
return true;
}
}
}
return false;
}
bool icycle(int src,vvi &adj,vector<bool> &vis,int par){
vis[src]=true;
for(auto i: adj[src]){
if(i!=par){
if(vis[i]){
return true;
}
if(!vis[i]&&icycle(i,adj,vis,src)){
return true;
}
}
}
return false;
}
int main(){
int n=4,m=3;
vvi adj(n);
rep(i,0,m){
int x,y;
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
bool cycle =false;
vector<bool> vis(n,false);
rep(i,0,n){
if(!vis[i]&& icycle(i,adj,vis,-1)){
cycle=true;
}
}
cout<<cycle;
}