JAVA Solution Explained in HINDI
https://youtu.be/_vKBnd5JAio\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote 
https://youtu.be/_vKBnd5JAio\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote the solution if you liked it.\n\n# Subscribe:- [ReelCoding](https://www.youtube.com/@reelcoding?sub_confirmation=1)\n\nSubscribe Goal:- 500\nCurrent Subscriber:- 468\n\n# Complexity\n- Time complexity: O(m*n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(m+n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    private List<Integer> merge(List<Integer> parent, List<Integer> child) {\n        List<Integer> sortedList = new ArrayList<>();\n        int n = parent.size(), m = child.size(), i = 0, j = 0;\n        while (i < n && j < m) {\n            if (parent.get(i) < child.get(j)) {\n                while (i + 1 < n && parent.get(i).equals(parent.get(i + 1))) i++;\n                sortedList.add(parent.get(i));\n                i++;\n            } else if (parent.get(i) > child.get(j)) {\n                while (j + 1 < m && child.get(j).equals(child.get(j + 1))) j++;\n                sortedList.add(child.get(j));\n                j++;\n            } else {\n                while (i + 1 < n && parent.get(i).equals(parent.get(i + 1))) i++;\n                while (j + 1 < m && child.get(j).equals(child.get(j + 1))) j++;\n                sortedList.add(child.get(j));\n                i++;\n                j++;\n            }\n        }\n\n        while (i < n) {\n            while (i + 1 < n && parent.get(i).equals(parent.get(i + 1))) i++;\n            sortedList.add(parent.get(i));\n            i++;\n        }\n\n        while (j < m) {\n            while (j + 1 < m && child.get(j).equals(child.get(j + 1))) j++;\n            sortedList.add(child.get(j));\n            j++;\n        }\n        return sortedList;\n    }\n\n    public List<List<Integer>> getAncestors(int n, int[][] edges) {\n        Map<Integer, List<Integer>> sortedAncestors = new HashMap<>();\n\n        int[] degree = new int[n];\n        Map<Integer, List<Integer>> gr = new HashMap<>();\n        for (int[] edge : edges) {\n            int u = edge[0], v = edge[1];\n            degree[v]++;\n            gr.computeIfAbsent(u, k -> new ArrayList<>()).add(v);\n        }\n\n        Queue<Integer> q = new ArrayDeque<>();\n        for (int node = 0; node < n; node++) {\n            if (degree[node] == 0) {\n                q.offer(node);\n            }\n        }\n\n        while (!q.isEmpty()) {\n            int sz = q.size();\n            while (sz-- > 0) {\n                int node = q.poll();\n\n                List<Integer> anc = sortedAncestors.getOrDefault(node, new ArrayList<>());\n                for (int child : gr.getOrDefault(node, new ArrayList<>())) {\n                    degree[child]--;\n                    List<Integer> par = Arrays.asList(node);\n                    sortedAncestors.put(child, merge(sortedAncestors.getOrDefault(node, new ArrayList<>()), sortedAncestors.getOrDefault(child, new ArrayList<>())));\n                    sortedAncestors.put(child, merge(sortedAncestors.get(child), par));\n                    if (degree[child] == 0) q.offer(child);\n                }\n            }\n        }\n\n        List<List<Integer>> ans = new ArrayList<>();\n        for (int node = 0; node < n; node++) {\n            ans.add(sortedAncestors.getOrDefault(node, new ArrayList<>()));\n        }\n        return ans;\n    }\n}\n```
['Java']