1
function DisjointSet(size) {5
for (let i = 0; i < size; i++) {9
this.find = function (x) {10
if (x === this.root[x]) {13
this.root[x] = this.find(this.root[x]);16
this.union = function (x, y) {17
const rootX = this.find(x);18
const rootY = this.find(y);19
if (rootX === rootY) return;21
if (this.rank[rootX] > this.rank[rootY]) {22
this.root[rootY] = this.root[rootX];23
} else if (this.rank[rootX] < this.rank[rootY]) {24
this.root[rootX] = this.root[rootY];26
this.root[rootY] = this.root[rootX];33
* @param {number[][]} isConnected36
var findCircleNum = function (isConnected) {37
const n = isConnected.length;38
const disjointSet = new DisjointSet(isConnected.length);39
for (let i = 0; i < n; i++) {40
for (let j = 0; j < n; j++) {41
if (isConnected[i][j]) {42
disjointSet.union(i, j);46
return disjointSet.size;