C++ Solution
\nclass Solution {\npublic:\n\n     \n    void dfs(int source , vector> &adjList , vector &visited)\n    {\n        visited[source] = 1;\n        for(int i=0; i
\nclass Solution {\npublic:\n\n     \n    void dfs(int source , vector<vector<int>> &adjList , vector<int> &visited)\n    {\n        visited[source] = 1;\n        for(int i=0; i<adjList[source].size(); i++)\n        {\n            int node = adjList[source][i];\n\n            if(visited[node] == 0) dfs(node, adjList,visited);\n        }\n    }  \n\n    \n   \n    int makeConnected(int n, vector<vector<int>>& connections) {\n\n           int s = connections.size();\n           vector<vector<int>> adjList(n);\n           int connected_components = 0;\n           vector<int> visited(n,0);\n\n           for(auto i : connections)\n           {\n               adjList[i[0]].push_back(i[1]);\n               adjList[i[1]].push_back(i[0]);\n           } \n\n           for(int i=0; i<n; i++)\n           {\n              if(visited[i]==0)\n              {\n                  dfs(i,adjList,visited);\n                  connected_components++;\n              }\n           }\n\n           return  s >= n-1 ? connected_components-1 : -1;\n    }\n};
['Depth-First Search', 'C++']