C++ || Topological Sorting || Khan's Algorithm || Cycle Detection
Code\n\nclass Solution {\npublic:\n    bool check_cycle(vector<vector<int>> &arr, int n,vector<vector<int>> &g,vector<int> &ans){\n        g.resize(n+1);\n     
# Code\n```\nclass Solution {\npublic:\n    bool check_cycle(vector<vector<int>> &arr, int n,vector<vector<int>> &g,vector<int> &ans){\n        g.resize(n+1);\n        vector<int> in(n+1,0);\n        for(auto item : arr){\n            in[item[1]]++;\n            g[item[0]].push_back(item[1]);\n        }\n        queue<int> q;\n        for(int i=1;i<in.size();i++){\n            if(in[i] == 0) q.push(i);\n        }\n        while(!q.empty()){\n            auto p = q.front();\n            q.pop();\n            ans.push_back(p);\n            for(auto item : g[p]){\n                if(in[item]>0){\n                    in[item]--;\n                    if(in[item] == 0) q.push(item);\n                }\n            }\n        }\n        return ans.size() != n;\n    }\n\n    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& r, vector<vector<int>>& c) {\n        vector<vector<int>> ans,gr,gc;\n        vector<int> row,col;\n        sort(r.begin(),r.end());\n        r.erase(unique(r.begin(),r.end()),r.end());\n\n        sort(c.begin(),c.end());\n        c.erase(unique(c.begin(),c.end()),c.end());\n\n        if(check_cycle(r,k,gr,row) or check_cycle(c,k,gc,col)) return ans;\n        ans.resize(k,vector<int>(k,0));\n        unordered_map<int,int> mpr,mpc;\n        for(int i=0;i<row.size();i++){\n            mpr[row[i]]=i;\n            mpc[col[i]]=i;\n        }\n        for(int i=1;i<=k;i++){\n            ans[mpr[i]][mpc[i]]=i;\n        }\n        return ans;\n    }\n};\n```
['Topological Sort', 'Queue', 'Ordered Map', 'C++']