forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2076.java
51 lines (48 loc) · 1.61 KB
/
_2076.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _2076 {
public static class Solution1 {
/**
* Credit: https://leetcode.com/SaveVMK/ on https://leetcode.com/contest/weekly-contest-267/ranking/
*/
public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {
int[] head = new int[n];
boolean[][] isr = new boolean[n][n];
for (int i = 0; i < n; i++) {
head[i] = i;
}
List<List<Integer>> ch = new ArrayList<>();
for (int i = 0; i < n; i++) {
ch.add(new ArrayList<>());
ch.get(i).add(i);
}
for (int[] res : restrictions) {
isr[res[0]][res[1]] = true;
isr[res[1]][res[0]] = true;
}
boolean[] ans = new boolean[requests.length];
for (int i = 0; i < requests.length; i++) {
int u = head[requests[i][0]];
int v = head[requests[i][1]];
if (u == v) {
ans[i] = true;
continue;
}
if (isr[u][v]) {
continue;
}
ans[i] = true;
for (int v2 : ch.get(v)) {
ch.get(u).add(v2);
head[v2] = u;
}
for (int j = 0; j < n; j++) {
isr[u][j] |= isr[v][j];
isr[j][u] |= isr[j][v];
}
}
return ans;
}
}
}