I tried to get the elements of a graph that are connected and the n print them in separated lines.But there seems to be some error in the code so please help me find it .I would be grateful if you could me where.It takes input of the no of vertices[range(0,V-1)]and edges then it gets all the separate islands in a set and then prints them...the error message is*" munmap_chunk(): invalid pointer: 0x00005633da4e3e88 ***"*
#include<bits/stdc++.h>
using namespace std;
void dfs(vector<int>*edges,int V,int*visited,unordered_set<int>*component,int index)
{
component->insert(index);
for(int i=0;i<edges[index].size();i++)
{
if(visited[edges[index][i]]==0)
{
visited[edges[index][i]]=1;
dfs(edges,V,visited,component,edges[index][i]);
}
}
}
unordered_set<unordered_set<int>*>*getComponents(vector<int>*edges,int V)
{
int*visited=new int[V]();
unordered_set<unordered_set<int>*>*components=new unordered_set<unordered_set<int>*>();
for(int i=0;i<V;i++)
{
if(visited[i]==0)
{
visited[i]=1;
unordered_set<int>*component=new unordered_set<int>;
dfs(edges,V,visited,component,i);
components->insert(component);
}
}
return components;
}
int main()
{
// cout<<"enter the no of vertices and the no of edges in your graph "<<endl;
int V,E;
cin>>V>>E;
vector<int>*edges=new vector<int>[V+1];
for(int i=0;i<E;i++)
{
int a,b;
cin>>a>>b;
edges[a].push_back(b);
edges[b].push_back(a);
}
unordered_set<unordered_set<int>*>*components=getComponents(edges,V);
unordered_set<unordered_set<int>*>::iterator it = components->begin();
while (it != components->end()) {
//cout<<"component::"<<it->begin()<<endl;
unordered_set<int>* component = *it;
unordered_set<int>::iterator it2 = component->begin();
while (it2 != component->end()) {
cout << *it2 << " ";
it2++;
}
cout << endl;
delete component;
it++;
}
delete components;
delete edges;
}