Python3 Solution || DFS Approach
Complexity\n- Time complexity:\nO(E+V)\n\n- Space complexity:\nO(V)\n\n# Code\n\nclass Solution:\n    def makeConnected(self, n: int, connections: List[List[int
# Complexity\n- Time complexity:\nO(E+V)\n\n- Space complexity:\nO(V)\n\n# Code\n```\nclass Solution:\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        adj=defaultdict(list)\n        for ele1,ele2 in connections:\n            adj[ele1].append(ele2)\n            adj[ele2].append(ele1)\n        self.cables=0\n        def dfs(i,visited,adj,parent):\n            if(i in visited):\n                self.cables+=1\n                return\n            visited.add(i)\n            for child in adj[i]:\n                if child!=parent:\n                    dfs(child,visited,adj,i)\n        c=0\n        visited=set()\n        for i in range(n):\n            if i not in visited:\n                c+=1\n                dfs(i,visited,adj,-1)\n        if(self.cables//2>=c-1):\n            return c-1\n        return -1\n\n```
['Depth-First Search', 'Breadth-First Search', 'Union Find', 'Graph', 'Python3']