TopoSort
\n\n# Code\n\nclass Solution {\npublic:\n        vector<vector<int>> buildMatrix(int k, vector<vector<int>> &rowConditions, vector<vector<int>> &colConditions)\
\n\n# Code\n```\nclass Solution {\npublic:\n        vector<vector<int>> buildMatrix(int k, vector<vector<int>> &rowConditions, vector<vector<int>> &colConditions)\n{\n    vector<int> adj1[k], adj2[k];\n    vector<int> indegree1(k, 0), indegree2(k, 0);\n    for (auto it : rowConditions)\n    {\n        int u = it[0] - 1;\n        int v = it[1] - 1;\n        adj1[u].push_back(v);\n        indegree1[v]++;\n    }\n\n    for (auto it : colConditions)\n    {\n        int u = it[0] - 1;\n        int v = it[1] - 1;\n        adj2[u].push_back(v);\n        indegree2[v]++;\n    }\n\n    vector<int> order1, order2;\n    queue<int> q;\n    for (int i = 0; i < k; i++)\n    {\n        if (indegree1[i] == 0)\n        {\n            q.push(i);\n        }\n    }\n\n    while (!q.empty())\n    {\n        int node = q.front();\n        q.pop();\n        order1.push_back(node);\n\n        for (auto it : adj1[node])\n        {\n            indegree1[it]--;\n            if (indegree1[it] == 0)\n            {\n                q.push(it);\n            }\n        }\n    }\n\n    if (order1.size() < k)\n    {\n        return {};\n    }\n\n    for (int i = 0; i < k; i++)\n    {\n        if (indegree2[i] == 0)\n        {\n            q.push(i);\n        }\n    }\n\n    while (!q.empty())\n    {\n        int node = q.front();\n        q.pop();\n        order2.push_back(node);\n\n        for (auto it : adj2[node])\n        {\n            indegree2[it]--;\n            if (indegree2[it] == 0)\n            {\n                q.push(it);\n            }\n        }\n    }\n\n    if (order2.size() < k)\n    {\n        return {};\n    }\n\n    vector<pair<int, int>> v(k);\n    for (int i = 0; i < order1.size(); i++)\n    {\n        v[order1[i]].first = i;\n    }\n\n    for (int i = 0; i < order2.size(); i++)\n    {\n        v[order2[i]].second = i;\n    }\n\n    vector<vector<int>> ans(k, vector<int>(k, 0));\n    for (int i = 0; i < k; i++)\n    {\n        int x = v[i].first;\n        int y = v[i].second;\n        ans[x][y] = i + 1;\n    }\n\n    return ans;\n}\n};\n```
['C++']