Java Topological Sort
```\n    public List> getAncestors(int n, int[][] edges) {\n        \n        \n        List> ancestorList = new ArrayList();\n        List> list = new ArrayLis
```\n    public List<List<Integer>> getAncestors(int n, int[][] edges) {\n        \n        \n        List<TreeSet<Integer>> ancestorList = new ArrayList();\n        List<List<Integer>> list = new ArrayList();\n\n        Queue<Integer> queue = new LinkedList();\n        \n        int degree[] = new int[n];\n        ArrayList<Integer>[] connections = new ArrayList[n];\n        \n        for(int i = 0;i<n;i++){\n            connections[i] = new ArrayList<Integer>();\n            ancestorList.add(new TreeSet<Integer>());\n        }\n        \n        for(int edge[]: edges){\n           connections[edge[0]].add(edge[1]);\n           degree[edge[1]]++;\n        }\n    \n        for(int i = 0;i<n;i++){\n            if(degree[i] == 0){\n                queue.add(i);\n            }\n        }\n        \n     \n        while(!queue.isEmpty()){\n            \n            int currNode = queue.poll();\n            for(int node : connections[currNode]){\n                ancestorList.get(node).addAll(new TreeSet(ancestorList.get(currNode)));\n                ancestorList.get(node).add(currNode);\n                \n                degree[node]--;\n                if(degree[node] == 0){\n                    queue.add(node);\n                }\n            }\n            \n        }\n        \n        \n        for(TreeSet<Integer> set : ancestorList){\n            list.add(new ArrayList<Integer>(set));\n        }\n        \n        return list;\n        \n        \n        \n    }
['Topological Sort', 'Java']