我尝试获取图形的单独孤岛的这段c ++代码有什么问题?

我试图获取连接的图的元素并将n打印在分开的行中。但是代码中似乎存在一些错误,所以请帮助我找到它。如果能在哪里我将不胜感激。输入顶点的数目[range(0,V-1)]和边,然后将其集合中的所有独立岛打印出来

#include<bits/stdc++.h>//i know i should use specific libraries sorry in advance
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>>E>>V;
    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-it.begin()<<endl;
        unordered_set<int>* component = *it;
        unordered_set<int>::iterator it2 = component->begin();
        while (it2 != component->end()) {
            cout << *it2 + 1 << " ";
            it2++;
        }
        cout << endl;
        delete component;
        it++;
    }
    delete components;
    delete edges;

}
评论