-
Notifications
You must be signed in to change notification settings - Fork 0
/
1462.课程表-iv.py
27 lines (26 loc) · 959 Bytes
/
1462.课程表-iv.py
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
class Solution:
def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]:
g = [[] for _ in range(numCourses)]
indgree = [0] * numCourses
isPre = [[False] * numCourses for _ in range(numCourses)]
for p in prerequisites:
indgree[p[1]] += 1
g[p[0]].append(p[1])
q = []
for i in range(numCourses):
if indgree[i] == 0:
q.append(i)
while len(q) > 0:
cur = q[0]
q.pop(0)
for ne in g[cur]:
isPre[cur][ne] = True
for i in range(numCourses):
isPre[i][ne] = isPre[i][ne] or isPre[i][cur]
indgree[ne] -= 1
if indgree[ne] == 0:
q.append(ne)
res = []
for query in queries:
res.append(isPre[query[0]][query[1]])
return res