Python | Cached DFS
\nclass Solution:\n    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        v_edges = [set() for i in range(n)]\n        for u, v
```\nclass Solution:\n    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        v_edges = [set() for i in range(n)]\n        for u, v in edges:\n            v_edges[v].add(u)\n        \n        @cache\n        def dfs(v):\n            children = set()\n            for v2 in v_edges[v]:\n                children.add(v2)\n                children |= dfs(v2)\n            return children\n        \n        ans = [dfs(v) for v in range(n)]\n        return [sorted(list(i)) for i in ans]\n```
[]