✅✅C++ || Easy Solution || 💯💯DFS Approach || Heavily Commented
\u2705\u2705C++ || Easy Solution || \uD83D\uDCAF\uD83D\uDCAFDFS Approach || Heavily Commented\n# Please Upvote as it really motivates me\n\n\nclass Solution {\n
## **\u2705\u2705C++ || Easy Solution || \uD83D\uDCAF\uD83D\uDCAFDFS Approach || Heavily Commented**\n# **Please Upvote as it really motivates me**\n\n```\nclass Solution {\npublic:\n\t//Depth First search function\n    void dfs(int node,vector<vector<int>>&g,vector<int>&vis){\n        vis[node]=0;\n        for(auto&child:g[node]){\n            if(vis[child]){\n                dfs(child,g,vis);\n            }\n        }\n    }\n    int makeConnected(int n, vector<vector<int>>& conn) {\n\t\t//When we have no. of edges less than n-1 we cant make it happen as tree will not form\n\t\t//so return -1\n        if(conn.size()<n-1){\n            return -1;\n        }\n\t\t//vector of vector for the adj list\n        vector<vector<int>>g(n);\n        for(int i=0;i<conn.size();i++){\n            g[conn[i][0]].push_back(conn[i][1]);\n            g[conn[i][1]].push_back(conn[i][0]);\n        }\n\t\t//vector of int for storing the visited nodes\n        vector<int>vis(n,1);\n\t\t//no of different connected components\n        int connectedComponents=0;\n\t\t//Traversing to all the different connected components nodes\n        for(int i=0;i<n;i++){\n\t\t//if not visited\n            if(vis[i]){\n                dfs(i,g,vis);\n\t\t\t\t//increase no of components\n                connectedComponents++;\n            }\n        }\n\t\t//return no of components-1\n        return connectedComponents-1;\n    }\n};\n```\n\n![image](https://assets.leetcode.com/users/images/7f423b57-81a2-46ce-9ab2-72ad38f668f7_1675480558.466273.png)\n
['Depth-First Search', 'Graph', 'C', 'C++']