forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFriendCircles.java
69 lines (61 loc) · 1.55 KB
/
FriendCircles.java
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
public class FriendCircles {
public int findCircleNum(int[][] M) {
int num = 0;
for (int i = 0; i < M.length; i++) {
if (M[i][i] == 1) {
dfs(M, i);
num++;
}
}
return num;
}
/**
* M[i][i] = 0表示第i个人我们已经访问过了
* 访问过的人无需重复访问
*/
private void dfs(int[][] M, int i) {
M[i][i] = 0;
for (int j = 0; j < M.length; j++) {
if (M[j][j] != 0 && M[i][j] == 1) {
dfs(M, j);
}
}
}
public int findCircleNum2(int[][] M) {
int n = M.length;
UnionFind uf = new UnionFind(n);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (M[i][j] == 1) {
uf.union(i, j);
}
}
}
return uf.count();
}
class UnionFind {
int[] path;
int count;
public UnionFind(int n) {
path = new int[n];
count = n;
for (int i = 0; i < n; i++) {
path[i] = i;
}
}
public int find(int i) {
while (i != path[i]) i = path[i];
return i;
}
public void union(int i, int j) {
int rootI = find(i);
int rootJ = find(j);
if (rootI == rootJ) return;
path[rootI] = rootJ;
count--;
}
public int count() {
return count;
}
}
}