-
Notifications
You must be signed in to change notification settings - Fork 65
/
1319.number-of-operations-to-make-network-connected.js
64 lines (63 loc) · 1.61 KB
/
1319.number-of-operations-to-make-network-connected.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* @lc app=leetcode.cn id=1319 lang=javascript
*
* [1319] Number of Operations to Make Network Connected
*/
// @lc code=start
/**
* @param {number} n
* @param {number[][]} connections
* @return {number}
* // 1. 将 p 和 q 连接
// 2. 判断 p 和 q 是否连通
// 3. 返回图中有多少个连通分量
*/
class UnionFind {
constructor(n) {
this.parent = new Array(n).fill(0).map((v, i) => i);
this.size = new Array(n).fill(1);
this.count = n;
}
find(x) {
let parent = this.parent;
while (x !== parent[x]) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
connected(p, q) {
const rootP = this.find(p);
const rootQ = this.find(q);
return rootP === rootQ;
}
union(p, q) {
let size = this.size;
let parent = this.parent;
const rootP = this.find(p);
const rootQ = this.find(q);
if (rootP === rootQ) return;
if (size[rootP] >= size[rootQ]) {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
} else {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
}
this.count--;
}
}
var makeConnected = function(n, connections) {
const union = new UnionFind(n);
let rest = 0;
for (let i = 0; i < connections.length; i++) {
const [a, b] = connections[i];
if (union.connected(a, b)) {
rest++;
} else {
union.union(a, b);
}
}
return (rest >= union.count - 1) ? union.count - 1 : -1;
};
// @lc code=end