-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.java
More file actions
75 lines (66 loc) · 2.08 KB
/
solution.java
File metadata and controls
75 lines (66 loc) · 2.08 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
72
73
74
75
import java.util.*;
class Solution {
static class DSU {
int[] p, sz;
DSU(int n) {
p = new int[n + 1];
sz = new int[n + 1];
for (int i = 0; i <= n; i++) {
p[i] = i;
sz[i] = 1;
}
}
int find(int x) {
return p[x] == x ? x : (p[x] = find(p[x]));
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (sz[a] < sz[b]) {
int tmp = a;
a = b;
b = tmp;
}
p[b] = a;
sz[a] += sz[b];
}
}
public int[] processQueries(int c, int[][] connections, int[][] queries) {
DSU dsu = new DSU(c);
for (int[] e : connections)
dsu.unite(e[0], e[1]);
Map<Integer, PriorityQueue<Integer>> heap = new HashMap<>();
for (int i = 1; i <= c; i++) {
int r = dsu.find(i);
heap.computeIfAbsent(r, k -> new PriorityQueue<>()).offer(i);
}
boolean[] offline = new boolean[c + 1];
List<Integer> res = new ArrayList<>(queries.length);
for (int[] q : queries) {
int t = q[0], x = q[1];
if (t == 2) {
offline[x] = true;
} else {
if (!offline[x]) {
res.add(x);
} else {
int r = dsu.find(x);
PriorityQueue<Integer> pq = heap.get(r);
if (pq == null) {
res.add(-1);
continue;
}
while (!pq.isEmpty() && offline[pq.peek()])
pq.poll();
res.add(pq.isEmpty() ? -1 : pq.peek());
}
}
}
int[] ans = new int[res.size()];
for (int i = 0; i < res.size(); i++)
ans[i] = res.get(i);
return ans;
}
}