Python short and clean. Disjoint-Set-Union (DSU) / Union-FInd.
Approach\n1. If number of connections is less than n - 1 to begin with, it\'s impossible to connect all computers. Hence return -1. \n\n2. Create a DSU (Disjoin
# Approach\n1. If number of `connections` is less than `n - 1` to begin with, it\'s impossible to connect all computers. Hence return `-1`. \n\n2. Create a `DSU (Disjoint-Set-Union)` with `n` nodes.\n\n3. Merge the sets by calling `union(u, v)` for every link `u <-> v` in `connectiona`.\n\n4. Get the count of disjoint-sets at the end, say `ds_count`.\n\n5. We need to move `ds_count - 1` links to connect them all.\n\n# Complexity\n- Time complexity: $$O(n + e)$$\n\n- Space complexity: $$O(n)$$\n\nwhere,\n`n is the number of computers`,\n`e is the number of connections between them`.\n\n# Code\n```\nclass Solution:\n    def makeConnected(self, n: int, connections: list[list[int]]) -> int:\n        if len(connections) < n - 1: return -1\n\n        dsu = DSU(range(n))\n        for u, v in connections: dsu.union(u, v)\n        return dsu.ds_count() - 1\n\nT = Hashable\nclass DSU:\n    def __init__(self, xs: Iterable[T] = ()) -> None:\n        self.parents: Mapping[T, T] = {x: x for x in xs}\n        self.sizes: Mapping[T, int] = {x: 1 for x in xs}\n        self.count: int = len(self.parents)\n\n    def find(self, u: T) -> T:\n        self.parents[u] = u if self.parents[u] == u else self.find(self.parents[u])\n        return self.parents[u]\n    \n    def union(self, u: T, v: T) -> None:\n        ur, vr = self.find(u), self.find(v)\n        if ur == vr: return\n        low, high = (ur, vr) if self.sizes[ur] < self.sizes[vr] else (vr, ur)\n        self.parents[low] = high\n        self.sizes[high] += self.sizes[low]\n        self.count -= 1\n    \n    def is_connected(self, u: T, v: T) -> bool:\n        return self.find(u) == self.find(v)\n    \n    def ds_count(self) -> int:\n        return self.count\n\n\n```
['Union Find', 'Graph', 'Python', 'Python3']