Created
January 26, 2021 16:40
-
-
Save dabasajay/e9eb735ae85d45b525b4faf8d9ab7da5 to your computer and use it in GitHub Desktop.
dsa/graphs/misc/ | desc: cycle in undirected graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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