#include <bits/stdc++.h>
using namespace std;
class Graph
{
map<int,vector > adj;
int v;
public:
Graph(int x)
{
v=x;
}
void addEdge(int x,int y)
{
adj[x].push_back(y);
adj[y].push_back(x);
}
bool cycle_helper(int node,bool visited[],int parent)
{
visited[node]=true;
for(auto it=adj[node].begin();it!=adj[node].end();it++)
{
if(visited[*it]==true && (*it)!=parent)
return true;
if(visited[*it]==false)
{
bool res=cycle_helper(*it,visited,node);
if(res==true)
return true;
}
}
return false;
}
bool cycle_undirected()
{
bool visited[v];
memset(visited,false,sizeof(visited));
return cycle_helper(0,visited,-1);
}
};
main()
{
Graph g(4);
g.addEdge(0,1);
g.addEdge(1,2);
g.addEdge(2,3);
g.addEdge(3,4);
// g.addEdge(4,0);
cout<<g.cycle_undirected();
}
What is the problem in this code ??