你总共需要上 numCourses
门课,课程编号依次为 0
到 numCourses-1
。你会得到一个数组 prerequisite
,其中 prerequisites[i] = [ai, bi]
表示如果你想选 bi
课程,你 必须 先选 ai
课程。
- 有的课会有直接的先修课程,比如如果想上课程
1
,你必须先上课程0
,那么会以[0,1]
数对的形式给出先修课程数对。
先决条件也可以是 间接 的。如果课程 a
是课程 b
的先决条件,课程 b
是课程 c
的先决条件,那么课程 a
就是课程 c
的先决条件。
你也得到一个数组 queries
,其中 queries[j] = [uj, vj]
。对于第 j
个查询,您应该回答课程 uj
是否是课程 vj
的先决条件。
返回一个布尔数组 answer
,其中 answer[j]
是第 j
个查询的答案。
示例 1:
输入:numCourses = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]] 输出:[false,true] 解释:课程 0 不是课程 1 的先修课程,但课程 1 是课程 0 的先修课程。
示例 2:
输入:numCourses = 2, prerequisites = [], queries = [[1,0],[0,1]] 输出:[false,false] 解释:没有先修课程对,所以每门课程之间是独立的。
示例 3:
输入:numCourses = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]] 输出:[true,true]
提示:
2 <= numCourses <= 100
0 <= prerequisites.length <= (numCourses * (numCourses - 1) / 2)
prerequisites[i].length == 2
0 <= ai, bi <= n - 1
ai != bi
- 每一对
[ai, bi]
都 不同 - 先修课程图中没有环。
1 <= queries.length <= 104
0 <= ui, vi <= n - 1
ui != vi
我们创建一个二维数组
接下来,我们遍历先修课程数组
然后,我们使用 Floyd 算法计算出所有节点对之间的可达性。
具体地,我们使用三重循环,首先枚举中间点
在计算完所有节点对之间的可达性之后,对于每一个查询
时间复杂度
class Solution:
def checkIfPrerequisite(
self, n: int, prerequisites: List[List[int]], queries: List[List[int]]
) -> List[bool]:
f = [[False] * n for _ in range(n)]
for a, b in prerequisites:
f[a][b] = True
for k in range(n):
for i in range(n):
for j in range(n):
if f[i][k] and f[k][j]:
f[i][j] = True
return [f[a][b] for a, b in queries]
class Solution {
public List<Boolean> checkIfPrerequisite(int n, int[][] prerequisites, int[][] queries) {
boolean[][] f = new boolean[n][n];
for (var p : prerequisites) {
f[p[0]][p[1]] = true;
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
f[i][j] |= f[i][k] && f[k][j];
}
}
}
List<Boolean> ans = new ArrayList<>();
for (var q : queries) {
ans.add(f[q[0]][q[1]]);
}
return ans;
}
}
class Solution {
public:
vector<bool> checkIfPrerequisite(int n, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {
bool f[n][n];
memset(f, false, sizeof(f));
for (auto& p : prerequisites) {
f[p[0]][p[1]] = true;
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
f[i][j] |= (f[i][k] && f[k][j]);
}
}
}
vector<bool> ans;
for (auto& q : queries) {
ans.push_back(f[q[0]][q[1]]);
}
return ans;
}
};
func checkIfPrerequisite(n int, prerequisites [][]int, queries [][]int) (ans []bool) {
f := make([][]bool, n)
for i := range f {
f[i] = make([]bool, n)
}
for _, p := range prerequisites {
f[p[0]][p[1]] = true
}
for k := 0; k < n; k++ {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
f[i][j] = f[i][j] || (f[i][k] && f[k][j])
}
}
}
for _, q := range queries {
ans = append(ans, f[q[0]][q[1]])
}
return
}
function checkIfPrerequisite(n: number, prerequisites: number[][], queries: number[][]): boolean[] {
const f = Array.from({ length: n }, () => Array(n).fill(false));
prerequisites.forEach(([a, b]) => (f[a][b] = true));
for (let k = 0; k < n; ++k) {
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
f[i][j] ||= f[i][k] && f[k][j];
}
}
}
return queries.map(([a, b]) => f[a][b]);
}
与方法一类似,我们创建一个二维数组
接下来,我们遍历先修课程数组
然后,我们使用拓扑排序计算出所有节点对之间的可达性。
定义一个队列
在计算完所有节点对之间的可达性之后,对于每一个查询
时间复杂度
class Solution:
def checkIfPrerequisite(
self, n: int, prerequisites: List[List[int]], queries: List[List[int]]
) -> List[bool]:
f = [[False] * n for _ in range(n)]
g = [[] for _ in range(n)]
indeg = [0] * n
for a, b in prerequisites:
g[a].append(b)
indeg[b] += 1
q = deque(i for i, x in enumerate(indeg) if x == 0)
while q:
i = q.popleft()
for j in g[i]:
f[i][j] = True
for h in range(n):
f[h][j] = f[h][j] or f[h][i]
indeg[j] -= 1
if indeg[j] == 0:
q.append(j)
return [f[a][b] for a, b in queries]
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;
}
}
class Solution {
public:
vector<bool> checkIfPrerequisite(int n, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {
bool f[n][n];
memset(f, false, sizeof(f));
vector<int> g[n];
vector<int> indeg(n);
for (auto& p : prerequisites) {
g[p[0]].push_back(p[1]);
++indeg[p[1]];
}
queue<int> q;
for (int i = 0; i < n; ++i) {
if (indeg[i] == 0) {
q.push(i);
}
}
while (!q.empty()) {
int i = q.front();
q.pop();
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.push(j);
}
}
}
vector<bool> ans;
for (auto& qry : queries) {
ans.push_back(f[qry[0]][qry[1]]);
}
return ans;
}
};
func checkIfPrerequisite(n int, prerequisites [][]int, queries [][]int) (ans []bool) {
f := make([][]bool, n)
for i := range f {
f[i] = make([]bool, n)
}
g := make([][]int, n)
indeg := make([]int, n)
for _, p := range prerequisites {
a, b := p[0], p[1]
g[a] = append(g[a], b)
indeg[b]++
}
q := []int{}
for i, x := range indeg {
if x == 0 {
q = append(q, i)
}
}
for len(q) > 0 {
i := q[0]
q = q[1:]
for _, j := range g[i] {
f[i][j] = true
for h := 0; h < n; h++ {
f[h][j] = f[h][j] || f[h][i]
}
indeg[j]--
if indeg[j] == 0 {
q = append(q, j)
}
}
}
for _, q := range queries {
ans = append(ans, f[q[0]][q[1]])
}
return
}
function checkIfPrerequisite(n: number, prerequisites: number[][], queries: number[][]): boolean[] {
const f = Array.from({ length: n }, () => Array(n).fill(false));
const g: number[][] = Array.from({ length: n }, () => []);
const indeg: number[] = Array(n).fill(0);
for (const [a, b] of prerequisites) {
g[a].push(b);
++indeg[b];
}
const q: number[] = [];
for (let i = 0; i < n; ++i) {
if (indeg[i] === 0) {
q.push(i);
}
}
while (q.length) {
const i = q.shift()!;
for (const j of g[i]) {
f[i][j] = true;
for (let h = 0; h < n; ++h) {
f[h][j] ||= f[h][i];
}
if (--indeg[j] === 0) {
q.push(j);
}
}
}
return queries.map(([a, b]) => f[a][b]);
}