DarkNerd || TopoLogical Sort
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    void buildGraph(vector<vector<int>>& vec, unordered_map<int, vector<int>>& graph){\n        int n=vec.size();\n        for(int i=0; i<n; i++){\n            graph[vec[i][0]].push_back(vec[i][1]);\n        }\n    }\n\n    void topo(int& k, unordered_map<int, vector<int>>& graph, vector<int>& ans){\n        int indegree[k+1];\n        memset(indegree, 0, sizeof(indegree));\n        for(auto& [parent, child]:graph){\n            for(int& i:child){\n                indegree[i]++;\n            }\n        }\n        queue<int> que;\n        for(int i=1; i<=k; i++){\n            if(indegree[i] == 0){\n                que.push(i);\n            }\n        }\n        while(!que.empty()){\n            int temp = que.front();\n            que.pop();\n\n            ans.push_back(temp);\n\n            for(int& i:graph[temp]){\n                indegree[i]--;\n                if(indegree[i] == 0){\n                    que.push(i);\n                }\n            }\n        }\n    }\n\n\npublic:\n    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& row, vector<vector<int>>& col) {\n        unordered_map<int, vector<int>> rowg;\n        unordered_map<int, vector<int>> colg;\n\n        buildGraph(row, rowg);\n        buildGraph(col, colg);\n\n        vector<int> r;\n        vector<int> c;\n\n        topo(k, rowg, r);\n        topo(k, colg, c);\n\n        if(r.empty() || c.empty() || r.size() != k || c.size() != k){\n            return {};\n        }\n\n        vector<vector<int>> res(k, vector<int>(k, 0));\n        for(int i=0; i<k; i++){\n            for(int j=0; j<k; j++){\n                if(r[i] == c[j]){\n                    res[i][j]=r[i];\n                }\n            }\n        }\n\n        return res;\n    }\n};\n```
['C++']