-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2316.java
53 lines (47 loc) · 1.6 KB
/
_2316.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
package com.fishercoder.solutions.thirdthousand;
import java.util.HashMap;
import java.util.Map;
public class _2316 {
public static class Solution1 {
public long countPairs(int n, int[][] edges) {
UnionFind uf = new UnionFind(n);
// this union is a first pass which doesn't union all nodes completely, if you set a
// debug point, you can clearly see that
for (int[] edge : edges) {
uf.union(edge[0], edge[1]);
}
Map<Integer, Integer> countMap = new HashMap<>();
// run i = 0 through to n - 1 again, and call find(), this will completely union all
// connected nodes
for (int i = 0; i < n; i++) {
int id = uf.find(i);
countMap.put(id, countMap.getOrDefault(id, 0) + 1);
}
long pairs = 0L;
long remaining = n;
for (int size : countMap.values()) {
pairs += size * (remaining - size);
remaining -= size;
}
return pairs;
}
class UnionFind {
int[] ids;
public UnionFind(int n) {
this.ids = new int[n];
for (int i = 0; i < n; i++) {
this.ids[i] = i;
}
}
public int find(int x) {
if (ids[x] != x) {
ids[x] = find(ids[x]);
}
return ids[x];
}
public void union(int x, int y) {
ids[find(x)] = find(y);
}
}
}
}