Cycle detection in undirected grapg using dfs

#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 ??

nodes are 5!! not 4 otherwise code is fine, also your code won’t work if graph has 2 or more connected components

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.