C++ Graph Solution
cpp\nint rowIdx[500], colIdx[500];\n\nclass Solution {\npublic:\n    vector<vector<int>> buildMatrix(int k, vector<vector<int>> &rowConditions, vector<vector<in
```cpp\nint rowIdx[500], colIdx[500];\n\nclass Solution {\npublic:\n    vector<vector<int>> buildMatrix(int k, vector<vector<int>> &rowConditions, vector<vector<int>> &colConditions) {\n        int n = rowConditions.size(), m = colConditions.size();\n        fill(rowIdx, rowIdx + k + 1, 0);\n        fill(colIdx, colIdx + k + 1, 0);\n        vector<vector<int>> rGraph(k + 1), cGraph(k + 1);\n        for (int i = 0; i < n; i++) {\n            rowIdx[rowConditions[i][1]]++;\n            rGraph[rowConditions[i][0]].push_back(rowConditions[i][1]);\n        }\n        for (int i = 0; i < m; i++) {\n            colIdx[colConditions[i][1]]++;\n            cGraph[colConditions[i][0]].push_back(colConditions[i][1]);\n        }\n        queue<int> cq, rq;\n        for (int i = 1; i <= k; i++) {\n            if (rowIdx[i] == 0) rq.push(i);\n            if (colIdx[i] == 0) cq.push(i);\n        }\n        vector<int> rows(k + 1), cols(k + 1);\n        int cnt = 0;\n        while (!cq.empty()) {\n            int i = cq.front();\n            cq.pop();\n            cols[i] = cnt++;\n            for (int j : cGraph[i]) {\n                colIdx[j]--;\n                if (colIdx[j] == 0) {\n                    cq.push(j);\n                }\n            }\n        }\n        if (cnt != k) return {};\n        cnt = 0;\n        while (!rq.empty()) {\n            int i = rq.front();\n            rq.pop();\n            rows[i] = cnt++;\n            for (int j : rGraph[i]) {\n                rowIdx[j]--;\n                if (rowIdx[j] == 0) {\n                    rq.push(j);\n                }\n            }\n        }\n        if (cnt != k) return {};\n        vector<vector<int>> res(k, vector<int>(k, 0));\n        for (int i = 1; i <= k; i++) {\n            res[rows[i]][cols[i]] = i;\n        }\n        return res;\n    }\n};\n```
['Graph', 'C', 'C++']