classSolution: deffindOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: node = {i:[] for i inrange(numCourses)} for p in prerequisites: node[p[0]].append(p[1]) visited = collections.OrderedDict() cycle = set() defdfs(index): if index in cycle: # in dfs path returnFalse if index in visited.keys(): # visited before, so doesn't need dfs any more # print(index) returnTrue # put this course in dfs cycle.add(index) for i in node[index]: ifnot dfs(i): returnFalse cycle.remove(index) # dfs is done and all course is ok! visited.update({index:0}) returnTrue for i inrange(numCourses): ifnot dfs(i): return [] return visited