[Python 3] Topological Sort O(V+E)
```\nclass Solution:\n    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        \n        in_degree = defaultdict(int)\n        gr
```\nclass Solution:\n    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        \n        in_degree = defaultdict(int)\n        graph = defaultdict(set)\n        \n        for A, B in edges:\n            graph[A].add(B)\n            in_degree[B] += 1\n        \n        q = deque()\n        \n        for i in range(n):\n            if i not in in_degree:\n                q.append(i)\n            \n        ans = [set() for i in range(n)] \n\n        while q:\n            node = q.popleft()\n            \n            for nei in graph[node]:\n\n                ans[nei].update(ans[node])  # my ancestors are your ancestors\n                ans[nei].add(node)          # i\'m your ancestor now, boy\n                \n                in_degree[nei] -= 1\n                if in_degree[nei] == 0:\n                    q.append(nei)\n        for i in range(len(ans)):\n            ans[i] = sorted(list(ans[i]))\n        return ans    \n            \n
[]