3
* @param {number[][]} connections8
this.root = new Array(n).fill().map((_, index) => index);12
if (x == this.root[x]) return x;13
return (this.root[x] = this.find(this.root[x]));16
const rootX = this.find(x);17
const rootY = this.find(y);19
this.root[rootY] = rootX;24
var makeConnected = function (n, connections) {25
// We need at least n - 1 cables to connect all nodes (like a tree).26
if (connections.length < n - 1) return -1;27
const uf = new UnionFind(n);28
for (const [a, b] of connections) {31
return uf.components - 1;