Simple O(V*E) solution without extra logic of sorting the lists
\n\n# Complexity\n- Time complexity:O(VE)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(VV)\n Add your space complexity here, e.g. O(n) \n
\n\n# Complexity\n- Time complexity:O(V*E)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(V*V)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\nvoid bfs(unordered_map<int,list<int>> &adj,int node,vector<vector<int>>&ans){\n    bool vis[1001];\n    memset(vis,0,sizeof(vis));\n    vis[node]=1;\n    queue<int> q;\n    q.push(node);\n    while(!q.empty()){\n        int size=q.size();\n        while(size--){\n            int x=q.front();\n            q.pop();\n            for(auto i:adj[x]){\n                if(!vis[i]){\n                    q.push(i);\n                    vis[i]=1;\n                    ans[i].push_back(node);\n                }\n            }\n        }\n    }\n\n}\n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n        vector<vector<int>> ans(n);\n        unordered_map<int,list<int>> adj;\n        for(auto i:edges){\n            adj[i[0]].push_back(i[1]);\n           // adj[i[1]].push_back(i[0]);\n        }\n        for(int i=0;i<n;i++){\n            bfs(adj,i,ans);\n           \n        }\n\n        // for(int i=0;i<n;i++)\n        // sort(ans[i].begin(),ans[i].end());\n        return ans;\n\n    }\n};\n```
['Breadth-First Search', 'Graph', 'Topological Sort', 'C++']