C++ | Easy | Topological Sorting
\n\nclass Solution {\npublic:\n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n        map<int,set<int>>mp;\n        vector<int>adj[n
\n```\nclass Solution {\npublic:\n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n        map<int,set<int>>mp;\n        vector<int>adj[n+1];\n        vector<int>indeg(n+1,0);\n        for(auto xt : edges)\n        {\n            adj[xt[0]].push_back(xt[1]);\n            indeg[xt[1]]++;\n        }\n        queue<int>q;\n        for(int i=0;i<n;i++)\n        {\n            if(indeg[i]==0)\n            {\n                q.push(i);\n            }\n        }\n        while(!q.empty())\n        {\n            int parent = q.front();q.pop();\n            for(auto child : adj[parent])\n            {\n                mp[child].insert(parent);\n                \n                for(auto xt : mp[parent])\n                {\n                    mp[child].insert(xt);\n                }\n                if(--indeg[child]==0)\n                {\n                    q.push(child);\n                }\n            }\n        }\n        \n        vector<vector<int>>ans;\n        for(int i=0;i<n;i++)\n        {\n            if(mp.find(i)==mp.end())\n            {\n                ans.push_back({});\n            }\n            else\n            {\n                vector<int> nodes(mp[i].begin(),mp[i].end());\n                ans.push_back(nodes);\n            }\n        }\n        return ans;\n        \n    }\n};\n```
['Graph', 'Topological Sort']