-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode547.java
More file actions
71 lines (64 loc) · 2.19 KB
/
LeetCode547.java
File metadata and controls
71 lines (64 loc) · 2.19 KB
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
65
66
67
68
69
70
71
public class LeetCode547 {
public static void main(String[] args) {
int[][] isConnected;
// 输入:isConnected = [[1,1,0],[1,1,0],[0,0,1]]
// 输出:2
isConnected = new int[][] { { 1, 1, 0 }, { 1, 1, 0 }, { 0, 0, 1 } };
System.out.println(new Solution547().findCircleNum(isConnected));
// 输入:isConnected = [[1,0,0],[0,1,0],[0,0,1]]
// 输出:3
isConnected = new int[][] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
System.out.println(new Solution547().findCircleNum(isConnected));
// 输入:isConnected = [[1,0,0,1],[0,1,1,0],[0,1,1,1],[1,0,1,1]]
// 输出:1
isConnected = new int[][] { { 1, 0, 0, 1 }, { 0, 1, 1, 0 }, { 0, 1, 1, 1 }, { 1, 0, 1, 1 } };
System.out.println(new Solution547().findCircleNum(isConnected));
}
}
class Solution547 {
int[] parents;
int[] ranks;
int count;
public int findCircleNum(int[][] isConnected) {
int nodeNum = isConnected.length;
// 初始化
count = nodeNum;
parents = new int[nodeNum];
ranks = new int[nodeNum];
for (int i = 0; i < nodeNum; i++) {
ranks[i] = 1;
parents[i] = i;
}
// 并查
for (int i = 0; i < nodeNum; i++) {
for (int j = i + 1; j < nodeNum; j++) {
if (isConnected[j][i] == 1) {
union(i, j);
}
}
}
return count;
}
private int find(int p) {
while (parents[p] != p) {
p = parents[p];
}
return p;
}
private void union(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (qRoot == pRoot) {
return;
}
if (ranks[pRoot] < ranks[qRoot]) {
parents[pRoot] = qRoot;
ranks[qRoot] = ranks[pRoot] + 1 <= ranks[qRoot] ? ranks[qRoot] : ranks[pRoot] + 1;
} else {
parents[qRoot] = pRoot;
ranks[pRoot] = ranks[qRoot] + 1 <= ranks[pRoot] ? ranks[pRoot] : ranks[qRoot] + 1;
}
count--;
// System.out.println(String.format("合并%d和%d, count变为%d", p, q, count));
}
}