Minimize count of connections required to be rearranged to make all the computers connected

  
#include
using namespace std;
  

void DFS(unordered_map& adj,
         int node, vector& visited)
{
    
    if (visited[node])
        return;
  
    
    visited[node] = true;
  
    
    for (auto x : adj[node]) {
  
        
        if (visited[x] == false)
            DFS(adj, x, visited);
    }
}
  

int makeConnectedUtil(int N,
                      int connections[][2],
                      int M)
{
    
    
    vector visited(N, false);
  
    
    unordered_map adj;
  
    
    int edges = 0;
  
    
    
    for (int i = 0; i < M; ++i) {                     adj[connections[i][0]].push_back(             connections[i][1]);         adj[connections[i][1]].push_back(             connections[i][0]);                     edges += 1;     }             int components = 0;        for (int i = 0; i < N; ++i) {                     if (visited[i] == false) {                             components += 1;                             DFS(adj, i, visited);         }     }             if (edges < N - 1)         return -1;             int redundant = edges - ((N - 1)                              - (components - 1));                  if (redundant >= (components – 1))
        return components – 1;
  
    return -1;
}
  

void makeConnected(int N, int connections[][2], int M)
{
    
    
    int minOps = makeConnectedUtil(
        N, connections, M);
  
    
    
    cout