Ruby solution beats 100% both run time and memory.
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n ^ 2 + n + m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n + m)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# @param {Integer} n\n# @param {Integer[][]} edges\n# @return {Integer[][]}\ndef get_ancestors(n, edges)\n    # Create adjacency list\n    adjacency_list = Array.new(n) { [] }\n\n    # Fill the adjacency list and indegree array based on the edges\n    indegree = Array.new(n, 0)\n    edges.each do |edge|\n        from = edge[0]\n        to = edge[1]\n        adjacency_list[from].push(to)\n        indegree[to] += 1\n    end\n\n    # Queue for nodes with no incoming edges (starting points for topological sort)\n    nodes_with_zero_indegree = []\n    indegree.each_with_index do |degree, i|\n        nodes_with_zero_indegree.push(i) if degree == 0\n    end\n\n    # List to store the topological order of nodes\n    topological_order = []\n    until nodes_with_zero_indegree.empty?\n        current_node = nodes_with_zero_indegree.shift\n        topological_order.push(current_node)\n\n        # Reduce indegree of neighboring nodes and add them to the queue\n        # if they have no more incoming edges\n        adjacency_list[current_node].each do |neighbor|\n            indegree[neighbor] -= 1\n            nodes_with_zero_indegree.push(neighbor) if indegree[neighbor] == 0\n        end\n    end\n\n    # Initialize the result list and set list for storing ancestors\n    ancestors_list = Array.new(n) { [] }\n    ancestors_set_list = Array.new(n) { Set.new }\n\n    # Fill the set list with ancestors using the topological order\n    topological_order.each do |node|\n        adjacency_list[node].each do |neighbor|\n            # Add immediate parent, and other ancestors.\n            ancestors_set_list[neighbor].add(node)\n            ancestors_set_list[neighbor].merge(ancestors_set_list[node])\n        end\n    end\n\n    # Convert sets to lists and sort them\n    ancestors_list.each_with_index do |list, i|\n        list.concat(ancestors_set_list[i].to_a)\n        list.sort!\n    end\n\n    return ancestors_list\nend\n```
['Ruby']