Участник:Alexander Denisenko/Eventual safe states

Материал из DISCOPAL
Перейти к: навигация, поиск
class Solution:
    def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]:
        state = collections.Counter()
 
        def dfs(node):
            if state[node]: 
                return state[node] == 2
 
            state[node] = 1
            for neighb in graph[node]:
                if state[neighb] == 1 or (not state[neighb] and not dfs(neighb)): 
                    return False
            state[node] = 2
            return True
 
        return list(filter(dfs, range(len(graph))))