Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
- g = defaultdict(list)
- visited = defaultdict(int)
- for v in range(numCourses):
- g[v] = []
- for a, b in prerequisites:
- g[b].append(a)
- def dfs(v):
- visited[v] = 1
- for n in g[v]:
- if visited[n] == 1 or (visited[n] == 0 and not dfs(n)):
- return False
- visited[v] = 2
- return True
- for v in g:
- if visited[v] == 0 and not dfs(v):
- return False
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement