Skip to content

Instantly share code, notes, and snippets.

@dabasajay
Created January 26, 2021 16:40
Show Gist options
  • Save dabasajay/e9eb735ae85d45b525b4faf8d9ab7da5 to your computer and use it in GitHub Desktop.
Save dabasajay/e9eb735ae85d45b525b4faf8d9ab7da5 to your computer and use it in GitHub Desktop.
dsa/graphs/misc/ | desc: cycle in undirected graph
#include <bits/stdc++.h>
using namespace std;
/* Using DFS to find a cycle in undirected graph */
int N;
vector<vector<int>> gph;
vector<bool> vis(N, false);
bool dfs(int src, int par, vector<bool>& vis){
vis[src] = true;
for(auto v : gph[src]) {
if(!vis[v]){
if(dfs(v, src, vis)) return true;
} else if(v != par) return true;
}
return false;
}
// dfs(0, vis);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment