-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
42 lines (40 loc) · 1.18 KB
/
Solution.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
class Solution {
private int[] p;
public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {
p = new int[n];
for (int i = 0; i < n; ++i) {
p[i] = i;
}
boolean[] ans = new boolean[requests.length];
int i = 0;
for (int[] req : requests) {
int u = req[0], v = req[1];
if (find(u) == find(v)) {
ans[i++] = true;
} else {
boolean valid = true;
for (int[] res : restrictions) {
int x = res[0], y = res[1];
if ((find(u) == find(x) && find(v) == find(y))
|| (find(u) == find(y) && find(v) == find(x))) {
valid = false;
break;
}
}
if (valid) {
p[find(u)] = find(v);
ans[i++] = true;
} else {
ans[i++] = false;
}
}
}
return ans;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}