207. Course Schedule
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
indeg = [0] * numCourses
M = defaultdict(list)
for v1 , v2 in prerequisites:
M[v2].append(v1)
indeg[v1] += 1
q = deque([i for i in range(len(indeg)) if indeg[i] == 0])
visited = set()
while q:
node = q.popleft()
if node in visited:
continue
visited.add(node)
for vv in M[node]:
indeg[vv] -= 1
if indeg[vv] == 0:
q.append(vv)
return len(visited) == numCourses