Python Elegant & Short | Reverse edges
\nfrom collections import defaultdict\nfrom typing import Iterable, List\n\n\nclass Solution:\n    def getAncestors(self, n: int, edges: List[List[int]]) -> Ite
```\nfrom collections import defaultdict\nfrom typing import Iterable, List\n\n\nclass Solution:\n    def getAncestors(self, n: int, edges: List[List[int]]) -> Iterable[List[int]]:\n        def dfs(u: int) -> set:\n            if not ancestors[u]:\n                for v in graph[u]:\n                    if v not in ancestors[u]:\n                        ancestors[u].update({v} | dfs(v))\n            return ancestors[u]\n\n        graph = defaultdict(set)\n        for u, v in edges:\n            graph[v].add(u)\n\n        ancestors = [set() for _ in range(n)]\n        for u in range(n):\n            if not ancestors[u]:\n                dfs(u)\n\n        return map(sorted, ancestors)\n```
['Python', 'Python3']