432 ms Beats 97.14%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        result = [set() for _ in range(n)]\n        indegrees = [0] * n\n        adj_list = collections.defaultdict(list)\n        for u, v in edges:\n            adj_list[u].append(v)\n            indegrees[v] += 1\n        queue = deque()\n        for i in range(len(indegrees)):\n            if not indegrees[i]:\n                queue.append(i)\n        while queue:\n            node = queue.popleft()\n            for neighbor in adj_list[node]:\n                result[neighbor].update(result[node], [node])\n                indegrees[neighbor] -= 1\n                if not indegrees[neighbor]:\n                    queue.append(neighbor)\n            result[node] = sorted(result[node])\n        return result\n#if you upvote this you will get a good news in 1 hour \n```
['Python3']