Kahn's Algo | Topological Sort | BFS | C++ | Short and Easy to Understand Code
\nvector<int> khansAlgo(vector<vector<int>> &r, int k){\n        vector<int> cnt(k+1, 0);\n        vector<int> adj[k+1];\n        for(auto x:r){\n            cn
```\nvector<int> khansAlgo(vector<vector<int>> &r, int k){\n        vector<int> cnt(k+1, 0);\n        vector<int> adj[k+1];\n        for(auto x:r){\n            cnt[x[1]]++;\n            adj[x[0]].push_back(x[1]);\n        }\n        vector<int> row;\n        queue<int> q;\n        for(int i=1;i<=k;i++){\n            if(cnt[i]==0){\n                q.push(i);\n            }\n        }\n        while(!q.empty()){\n            int t = q.front();\n            q.pop();\n            row.push_back(t);\n            for(auto x:adj[t]){\n                cnt[x]--;\n                if(cnt[x]==0){\n                    q.push(x);\n                }\n            }\n        }\n        return row;\n    }\n    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& r, vector<vector<int>>& c) {\n        vector<vector<int>> res(k, vector<int>(k, 0));\n        \n        vector<int> row = khansAlgo(r, k);\n        if(row.size()!=k) return {};\n        \n        vector<int> col = khansAlgo(c, k);\n        if(col.size()!=k) return {};\n        \n        vector<int> idx(k+1,0);\n        for(int j=0;j<col.size();j++){\n            idx[col[j]] = j;\n        }\n        for(int i=0;i<k;i++){\n            res[i][idx[row[i]]] = row[i];\n        }\n        return res;\n    }\n```
['Breadth-First Search', 'Topological Sort', 'C']