What wrong in my code

#include<bits/stdc++.h>
#include
using namespace std;

class graph{
public:
map<int,list>mp;

int sum=0;
int e,v,tem,ro;
graph(int p,int q,int r,int s){
  e=p;
  v=q;
  tem=r;
  ro=s;
  }

void addedge(int a,int b){
mp[a].push_back(b);
mp[b].push_back(a);
}

int dfshelper(int src,bool visited[]){
       visited[(int)src]=true;
       int count=1;
      for(auto it:mp[(int)src]){
          if(!visited[it]){
          count+= dfshelper(it,visited);
              
          }
         
      }
      return count;
  }

  int dfs(){
       bool visited[v]={false};
       int count=0;
      for(auto i:mp){
          if(!visited[(int)i.first]){
              
      count=dfshelper((int)i.first,visited);
      
              count--;
          
          if(ro>tem)
            {
                sum+=tem*(count+1);
            }
            else{
            sum+=(count*ro + tem);

        }
      }}
      return sum;
  }

};
int main(){
int t,n,m,a,b,i;
cin>>t;

while(t--)
{
    cin>>n>>m>>a>>b;
    graph g(m,n,a,b);
for(i=0;i<m;i++)
{
    int p,q;
    cin>>p>>q;
    g.addedge(p,q);
}
cout<<g.dfs()<<endl;

}

}

Hello @dineshjani,

Mistakes:

  1. // map<int,list>mp;
    map<int,list>mp;

  2. Y=As you have used a map.
    So, your code will fail to consider the isolated nodes.
    Exmple:
    1
    4 2 2 1
    1 3
    3 2
    There are 4 nodes 1,2,3,4
    But the roads can only reach 1 2 3
    So, you code will never consider the node 4.

Solution:
Rather us a list of list.

Hope, this would help.
Give a like if you are satisfied.

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.