python union find
python\nclass Solution:\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        if len(connections) < n - 1:\n            return -1\
```python\nclass Solution:\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        if len(connections) < n - 1:\n            return -1\n        p = {x : x for x in range(n)}\n        def find(x):\n            while x != p[x]:\n                p[x] = p[p[x]]\n                x = p[x]\n            return x\n           \n        for x, y in connections:\n            p[find(x)] = find(y)\n        \n        return len({find(x) for x in range(n)}) - 1\n```
[]