100% easy solution 🔥🔥✅✅💯💯DFS, count the number of disconnected components in graph
Intuition\n Describe your first thoughts on how to solve this problem. \nthere is n-1 edges required to connect n nodes in graph if number of edges less than n-
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nthere is n-1 edges required to connect n nodes in graph if number of edges less than n-1 graph cannot be connected , simply return -1.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nthis problem is application of count number of components in graph.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nwe use dfs here so the time complexity is simply \n$$O(V+E)$$.\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nhere  V: no. of vertices in graph\n      E: no. of edges in graph \nby dfs - $$O(V)$$\nby adjacency list - $$O(V+E)$$\n\noverall time complexity is = $$O(V+E)$$\n\n# Code\n```\nclass Solution {\npublic:\n\n    void dfs(vector<int> adj[],vector<bool> &visited,int src){\n        visited[src] = true;\n        for(auto i:adj[src]){\n            if(!visited[i]){\n                dfs(adj,visited,i);\n            }\n        }\n     }\n\n    int makeConnected(int n, vector<vector<int>>& connections) {\n        // if no. of edges is less than n-1 , there is no way to graph connected\n        if(connections.size()<n-1) return -1;\n\n        // this problem is application of count the no. of components in graph\n        // creating adjacency list\n        vector<int> adj[n];\n        for(auto v:connections){\n            adj[v[0]].push_back(v[1]);\n            adj[v[1]].push_back(v[0]);\n        }\n\n        //create a visited arrray to track all visited node\n        vector<bool> visited(n,false);\n\n        //count the no. of disconnected components and return no. of components-1 as the answer\n        int ans = 0;\n        for(int i=0;i<n;i++){\n            if(!visited[i]){\n                dfs(adj,visited,i);\n                ans++;\n            }\n        }\n        return ans-1;\n    }\n};\n```
['C++']