Javascript | DFS
\n/**\n * @param {number} n\n * @param {number[][]} edges\n * @return {number[][]}\n */\nvar getAncestors = function(n, edges) {\n    const arr = Array(n).fill(
```\n/**\n * @param {number} n\n * @param {number[][]} edges\n * @return {number[][]}\n */\nvar getAncestors = function(n, edges) {\n    const arr = Array(n).fill(1).map(el => new Set());\n    const seen = new Set();\n    const egs = [];\n    \n    for(const [from, to] of edges)\xA0{\n        if(egs[from]) egs[from].push(to)\n        else egs[from] = [to]\n    }\n\n    const dfs = (curr, by) => {\n        if(curr != by) {\n            arr[curr].add(by);\n        }\n        if(seen.has(curr)) return;\n        if(!egs[curr]) return;\n        seen.add(curr)\n        for(const dest of egs[curr]) {\n            dfs(dest, by);\n        }\n    }\n    for(let i = 0; i < n; i++) {\n        dfs(i, i);\n        seen.clear();\n    }\n    \n    return arr.map(set => {\n        return Array.from(set);\n    })\n\n};\n```
[]