Topological sort | cpp solution
The idea is simple we can use topological sort .\n\nclass Solution {\npublic:\n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n      
The idea is simple we can use topological sort .\n```\nclass Solution {\npublic:\n    vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n        vector<int>adj[n];\n\t\t\n        vector<int>in(n,0);\n        //we will create geraph first\n\t\tfor(auto it : edges)\n        {\n            adj[it[0]].push_back(it[1]);\n            in[it[1]]++;      //count of incoming edges\n        }\n\t\n        queue<int>q;\n        \n        for(int i=0; i<n; i++)\n        {\n\t\t\t//We will push the edges ifirst which has zero incoming edges\n            if(in[i] == 0)\n                q.push(i);\n        }\n\t\t//there may be repeatation of node so we will use the set for unique value\n\t\t\n        vector<set<int>>ans(n);\n        while(!q.empty()){\n            int si = q.size();\n            while(si--)\n            {\n                int top = q.front();\n                for(int i : adj[top])\n                {\n\t\t\t\t//when we visit the node we will decrease the incoming egde count\n                    in[i]--;\n                     ans[i].insert(top);\n\t\t\t\t\t \n                       if(in[i] == 0)\n                           q.push(i);\n                        for(auto j : ans[top])\n                        {\n                            ans[i].insert(j);\n                        }\n                    \n                }\n                q.pop();\n            }\n        }\n        vector<vector<int>>res(n);\n        for(int i=0; i<n; i++)\n        {\n            for(auto j : ans[i])\n            {\n                res[i].push_back(j);\n            }\n        }\n        return res;\n    }\n};\n```
['Topological Sort', 'C++']