-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSolution.java
More file actions
59 lines (51 loc) · 1.51 KB
/
Solution.java
File metadata and controls
59 lines (51 loc) · 1.51 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
import java.util.*;
class Solution {
class DSU {
int[] par, rank;
public DSU(int n) {
par = new int[n + 1];
rank = new int[n + 1];
Arrays.fill(rank, 1);
for (int i = 0; i <= n; i++) par[i] = i;
}
public int find_set(int v) {
if (v == par[v]) return v;
return par[v] = find_set(par[v]); // Path compression
}
public void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
par[b] = a;
rank[a] += rank[b];
}
}
}
public List<Integer> minimumCost(int n, int[][] edges, int[][] query) {
DSU ds = new DSU(n);
int[] ands = new int[n + 1];
Arrays.fill(ands, -1);
for (int[] it : edges) {
ds.union_sets(it[0], it[1]);
}
for (int[] it : edges) {
int root = ds.find_set(it[0]);
int cur = ands[root];
ands[root] = (cur == -1) ? it[2] : (cur & it[2]);
}
List<Integer> ans = new ArrayList<>();
for (int[] it : query) {
if (ds.find_set(it[0]) == ds.find_set(it[1])) {
ans.add(ands[ds.find_set(it[0])]);
} else {
ans.add(-1);
}
}
return ans;
}
}