Java Solution
Code\n\nclass Solution {\n    public List<List<Integer>> getAncestors(int n, int[][] edges) {\n        List<List<Integer>> res=new ArrayList<>();\n        for(i
# Code\n```\nclass Solution {\n    public List<List<Integer>> getAncestors(int n, int[][] edges) {\n        List<List<Integer>> res=new ArrayList<>();\n        for(int i=0; i<n; i++){\n            res.add(new ArrayList<>());\n        } \n\n        ArrayList<Integer>[] graph=new ArrayList[n];\n        for(int i=0; i<n; i++){\n            graph[i]=new ArrayList<>();\n        }   \n        for(int[] edge: edges){\n            graph[edge[0]].add(edge[1]);\n        }\n        for(int i=0; i<n; i++){\n            dfs(graph,i,i,res,new boolean[n]);\n        }\n        return res;\n    }\n    public void dfs( ArrayList<Integer>[] graph, int parent, int curr, List<List<Integer>> res,boolean[] visit){\n        visit[curr]=true;\n        for(int i=0; i< graph[curr].size(); i++){\n            int dest=graph[curr].get(i);\n            if(!visit[dest]){\n                res.get(dest).add(parent);\n                dfs(graph,parent,dest,res,visit);\n            }\n        }\n    }\n}\n```
['Java']