-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution2.java
35 lines (35 loc) · 1.03 KB
/
Solution2.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
class Solution {
public List<Boolean> checkIfPrerequisite(int n, int[][] prerequisites, int[][] queries) {
boolean[][] f = new boolean[n][n];
List<Integer>[] g = new List[n];
int[] indeg = new int[n];
Arrays.setAll(g, i -> new ArrayList<>());
for (var p : prerequisites) {
g[p[0]].add(p[1]);
++indeg[p[1]];
}
Deque<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
if (indeg[i] == 0) {
q.offer(i);
}
}
while (!q.isEmpty()) {
int i = q.poll();
for (int j : g[i]) {
f[i][j] = true;
for (int h = 0; h < n; ++h) {
f[h][j] |= f[h][i];
}
if (--indeg[j] == 0) {
q.offer(j);
}
}
}
List<Boolean> ans = new ArrayList<>();
for (var qry : queries) {
ans.add(f[qry[0]][qry[1]]);
}
return ans;
}
}