Detectar ciclo en un gráfico no dirigido
//function snippet using DFS
bool dfs(int node,int parent, vector<int> adj[], vector<bool> &check)
{
check[node] = true;
for(auto n1 :adj[node])
{
if(!check[n1])
if(dfs(n1, node, adj, check)) return true;
else if(n1 != parent) return true;
}
return false;
}
bool isCycle(int V, vector<int> adj[]) {
vector<bool>check(V, 0);
//for all the commponents
for(int i =0; i < V; i++)
{
if(!check[i])
if (dfs(i, -1, adj, check)) return true;// if a component has a cycle then graph has a cycle
}
return false;
}
subham