Solution By Dare2Solve | Detailed Explanation | Beats 100% | Clean Code
Explanation []\nauthorslog.vercel.app/blog/vKnWmPK4AL\n\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n    vector<vector<int>> getAncestors(int n, vector<vecto
```Explanation []\nauthorslog.vercel.app/blog/vKnWmPK4AL\n```\n\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n        vector<set<int>> map(n);\n        vector<vector<int>> res(n);\n        vector<bool> visited(n);\n\n        function<void(int, int)> dfs = [&](int v, int p) {\n            visited[v] = true;\n            if (v != p) res[v].push_back(p);\n            for (int x : map[v]) {\n                if (!visited[x]) dfs(x, p);\n            }\n        };\n\n        for (const auto& edge : edges) {\n            map[edge[0]].insert(edge[1]);\n        }\n\n        for (int i = 0; i < n; ++i) {\n            fill(visited.begin(), visited.end(), false);\n            dfs(i, i);\n        }\n\n        return res;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        map = [set() for _ in range(n)]\n        res = [[] for _ in range(n)]\n        visited = [False] * n\n\n        def dfs(v: int, p: int):\n            visited[v] = True\n            if v != p:\n                res[v].append(p)\n            for x in map[v]:\n                if not visited[x]:\n                    dfs(x, p)\n\n        for u, v in edges:\n            map[u].add(v)\n\n        for i in range(n):\n            visited = [False] * n\n            dfs(i, i)\n\n        return res\n```\n\n```java []\nclass Solution {\n    public List<List<Integer>> getAncestors(int n, int[][] edges) {\n        List<Set<Integer>> map = new ArrayList<>();\n        List<List<Integer>> res = new ArrayList<>();\n        boolean[] visited = new boolean[n];\n\n        for (int i = 0; i < n; i++) {\n            map.add(new HashSet<>());\n            res.add(new ArrayList<>());\n        }\n\n        for (int[] edge : edges) {\n            map.get(edge[0]).add(edge[1]);\n        }\n\n        for (int i = 0; i < n; i++) {\n            Arrays.fill(visited, false);\n            dfs(i, i, map, res, visited);\n        }\n\n        return res;\n    }\n\n    private void dfs(int v, int p, List<Set<Integer>> map, List<List<Integer>> res, boolean[] visited) {\n        visited[v] = true;\n        if (v != p) res.get(v).add(p);\n        for (int x : map.get(v)) {\n            if (!visited[x]) {\n                dfs(x, p, map, res, visited);\n            }\n        }\n    }\n}\n```\n\n```javascript []\n/**\n * @param {number} n\n * @param {number[][]} edges\n * @return {number[][]}\n */\nvar getAncestors = function (n, edges) {\n    var map = Array.from({ length: n }, () => new Set());\n    var res = Array.from({ length: n }, () => []);\n    var visited = new Uint8Array(n);\n\n    var dfs = (v, p) => {\n        visited[v] = 1;\n        if (v !== p) res[v].push(p);\n        for (var x of map[v]) if (!visited[x]) dfs(x, p);\n    };\n\n    edges.forEach(([u, v]) => map[u].add(v));\n    for (let i = 0; i < n; ++i) visited.fill(0), dfs(i, i);\n    return res;\n};\n\n```
['Depth-First Search', 'Graph', 'Topological Sort', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']