[Python3] Very Direct and Simple BFS
Brute force BFS\n\n    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        # contruct a graph from children to parent\n        g
Brute force BFS\n\n    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        # contruct a graph from children to parent\n        graph = defaultdict(list)\n        for a, b in edges:\n            graph[b].append(a)\n        \n        ans = []\n        for i in range(n):\n            queue = deque([i])\n            visited = set()\n            while queue:\n                node = queue.popleft()\n                for nei in graph[node]:\n                    if nei not in visited:\n                        queue.append(nei)\n                        visited.add(nei)\n            ans.append(sorted(list(visited)))\n        return ans\n                    \n
['Breadth-First Search', 'Graph', 'Python3']