✅C++ || EASY DFS Solution
Intuition \nThe main idea is here to get the number of connected components in graph \n Describe your first thoughts on how to solve this problem. \n\n# Approac
# Intuition \nThe main idea is here to get the number of connected components in graph \n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach \nDFS traversal to find number of connected components\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: **O(n)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(n)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    void dfs(vector<int> ad[],int i,vector<int>&vis){\n        for(auto k : ad[i]){\n            if(!vis[k]){\n                vis[k]=1;\n                dfs(ad,k,vis);\n            }\n        }\n    }\n    int makeConnected(int n, vector<vector<int>>& v) {\n        if(n-1>v.size())return -1;\n        vector<int> ad[n];\n        vector<int>vis(n,0);\n        int c=0;\n        for(auto i:v){\n            ad[i[0]].push_back(i[1]);\n            ad[i[1]].push_back(i[0]);\n        }\n        for(int i=0;i<n;i++){\n              if(!vis[i]){\n                  vis[i]=1;\n                  c++;\n                  dfs(ad,i,vis);\n              }\n        }\n        return c-1;\n    }\n};\n```\n![upvote (3).jpg](https://assets.leetcode.com/users/images/42607605-5d40-4e5a-8ea4-faaba4fd647d_1679556252.9584877.jpeg)\n
['Depth-First Search', 'Graph', 'C++']