Beats 91.33%|| VERY EASY SOLUTION✅✅||with comments for easy understanding
Complexity\n- Time complexity:\nO(V\u22C5(V+E))\n\n- Space complexity:\nO(V^2+E)\n\n# Code\n\nclass Solution {\npublic:\n    vector<vector<int>> getAncestors(in
# Complexity\n- Time complexity:\nO(V\u22C5(V+E))\n\n- Space complexity:\nO(V^2+E)\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n        vector<vector<int>> adj(n);\n        vector<vector<int>> ans(n);\n\n        // Build the adjacency list\n        for (int i = 0; i < edges.size(); i++) {\n            adj[edges[i][0]].push_back(edges[i][1]);\n        }\n\n        // Helper function to perform DFS\n        function<void(int, int)> dfs = [&](int node, int ancestor) {\n            for (int child : adj[node]) {\n                if (ans[child].empty() || ans[child].back() != ancestor) {\n                    ans[child].push_back(ancestor);\n                    dfs(child, ancestor);\n                }\n            }\n        };\n\n        // Find ancestors for each node\n        for (int i = 0; i < n; i++) {\n            dfs(i, i);\n        }\n\n        return ans;\n    }\n};\n```\n**PLEASE UPVOTE \u2B06**
['C++']