Python 3 union find easy to understand
\n\nclass Solution:``\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        parent = list(range(n))\n        self.count = n\n     
\n```\nclass Solution:``\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        parent = list(range(n))\n        self.count = n\n        self.redundant = 0\n        def find(x):\n            if x != parent[x]:\n                parent[x] = find(parent[x])\n            return parent[x]\n        def union(x,y):\n            rootx = find(x)\n            rooty = find(y)\n            if rootx != rooty:\n                parent[rootx] = rooty\n                self.count -= 1\n            else:\n                self.redundant +=1\n        \n        for x, y in connections:\n            union(x, y)\n        \n        if self.redundant >= self.count - 1:\n            return self.count - 1\n        else:\n            return -1\n\t\t\t\n```
['Union Find', 'Python', 'Python3']