Python union find solution
Intuition\n Describe your first thoughts on how to solve this problem. \nUse Union find to count the redundant connections and connected clusters.\n\n# Approach
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse Union find to count the redundant connections and connected clusters.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(nlog(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\n# Code\n```\nclass Solution:\n    def makeConnected(self, n: int, connections: List[List[int]]) -> int:\n        count = 0           # number of redundant connections\n        network = [i for i in range(n)]\n\n        def find(x):\n            if network[x] != x:\n                network[x] = find(network[x])\n            return network[x]\n        \n        def union(a, b):\n            # return True if 2 nodes are already connected.\n            root_a = find(a)\n            root_b = find(b)\n            network[root_a] = root_b\n            return True if root_a == root_b else False\n        \n        for link in connections:\n            if union(link[0], link[1]):\n                count += 1\n\n        # count the number of connected clusters\n        a = len(set([find(i) for i in range(n)]))\n        return -1 if a - 1 > count else a - 1\n```
['Union Find', 'Python3']