<p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>

<ul>
	<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
	<li>a 2D integer array <code>colConditions</code> of size <code>m</code> where <code>colConditions[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>.</li>
</ul>

<p>The two arrays contain integers from <code>1</code> to <code>k</code>.</p>

<p>You have to build a <code>k x k</code> matrix that contains each of the numbers from <code>1</code> to <code>k</code> <strong>exactly once</strong>. The remaining cells should have the value <code>0</code>.</p>

<p>The matrix should also satisfy the following conditions:</p>

<ul>
	<li>The number <code>above<sub>i</sub></code> should appear in a <strong>row</strong> that is strictly <strong>above</strong> the row at which the number <code>below<sub>i</sub></code> appears for all <code>i</code> from <code>0</code> to <code>n - 1</code>.</li>
	<li>The number <code>left<sub>i</sub></code> should appear in a <strong>column</strong> that is strictly <strong>left</strong> of the column at which the number <code>right<sub>i</sub></code> appears for all <code>i</code> from <code>0</code> to <code>m - 1</code>.</li>
</ul>

<p>Return <em><strong>any</strong> matrix that satisfies the conditions</em>. If no answer exists, return an empty matrix.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/07/06/gridosdrawio.png" style="width: 211px; height: 211px;" />
<pre>
<strong>Input:</strong> k = 3, rowConditions = [[1,2],[3,2]], colConditions = [[2,1],[3,2]]
<strong>Output:</strong> [[3,0,0],[0,0,1],[0,2,0]]
<strong>Explanation:</strong> The diagram above shows a valid example of a matrix that satisfies all the conditions.
The row conditions are the following:
- Number 1 is in row <u>1</u>, and number 2 is in row <u>2</u>, so 1 is above 2 in the matrix.
- Number 3 is in row <u>0</u>, and number 2 is in row <u>2</u>, so 3 is above 2 in the matrix.
The column conditions are the following:
- Number 2 is in column <u>1</u>, and number 1 is in column <u>2</u>, so 2 is left of 1 in the matrix.
- Number 3 is in column <u>0</u>, and number 2 is in column <u>1</u>, so 3 is left of 2 in the matrix.
Note that there may be multiple correct answers.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> k = 3, rowConditions = [[1,2],[2,3],[3,1],[2,3]], colConditions = [[2,1]]
<strong>Output:</strong> []
<strong>Explanation:</strong> From the first two conditions, 3 has to be below 1 but the third conditions needs 3 to be above 1 to be satisfied.
No matrix can satisfy all the conditions, so we return the empty matrix.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= k &lt;= 400</code></li>
	<li><code>1 &lt;= rowConditions.length, colConditions.length &lt;= 10<sup>4</sup></code></li>
	<li><code>rowConditions[i].length == colConditions[i].length == 2</code></li>
	<li><code>1 &lt;= above<sub>i</sub>, below<sub>i</sub>, left<sub>i</sub>, right<sub>i</sub> &lt;= k</code></li>
	<li><code>above<sub>i</sub> != below<sub>i</sub></code></li>
	<li><code>left<sub>i</sub> != right<sub>i</sub></code></li>
</ul>

class Solution {
public:
  /*  bool dfs(int i,vector<vector<int>>&adj,int & c,map<int,int>&h,vector<bool>&visited,map<int,int>&h1){
        visited[i]=1;
        h1[i]=1;
        for(int j:adj[i]){
            if(!visited[j]){
                bool check=dfs(j,adj,c,h,visited,h1);
                if(check==0){
                    return 0;
                }
            }
            else{
                if(h1.count(j)){
                    return 0;
                }
            }
        }
        h1.erase(i);
        h[i]=c;
        c++;
        return 1;
    }
    bool helper(vector<vector<int>>&adj,vector<int>&indegree,map<int,int>&h){
        int k=adj.size()-1;
        queue<int>q;
        int c=0;
        for(int i=1;i<=k;i++){
            if(indegree[i]==0){
                q.push(i);
            }
        }
        int row=0;
        while(!q.empty()){
            int size=q.size();
            for(int i=0;i<size;i++){
                int a=q.front();
                cout<<a<<" "<<row<<endl;
                q.pop();
                c++;
                h[a]=row;
                for(int j:adj[a]){
                    indegree[j]--;
                    if(indegree[j]==0){
                        q.push(j);
                    }
                }
            }
            row++;
        }
        return c==k;
    }
    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {
        vector<vector<int>>adj1(k+1);
        vector<vector<int>>adj2(k+1);
        vector<int>indegree1(k+1,0);
        vector<int>indegree2(k+1,0);
        for(int i=0;i<rowConditions.size();i++){
            int a=rowConditions[i][0];
            int b=rowConditions[i][1];
            adj1[a].push_back(b);
            indegree1[b]++;
        }
        for(int i=0;i<colConditions.size();i++){
            int a=colConditions[i][0];
            int b=colConditions[i][1];
            adj2[a].push_back(b);
            indegree2[b]++;
        }
        map<int,int>h1;
        map<int,int>h2;
        bool c=helper(adj1,indegree1,h1);
        if(c==0){
            vector<vector<int>>v;
            return v;
        }
        c=helper(adj2,indegree2,h2);
        if(c==0){
            vector<vector<int>>v;
            return v;
        }
        vector<vector<int>>ans(k,vector<int>(k,0));
        for(int i=1;i<=k;i++){
            int a=h1[i];
            int b=h2[i];
            ans[a][b]=i;
        }
        return ans;
    }*/
    bool helper(vector<vector<int>>& adj, vector<int>& indegree, map<int, int>& position) {
    int k = adj.size() - 1;
    queue<int> q;
    int processed = 0;
    for (int i = 1; i <= k; i++) {
        if (indegree[i] == 0) {
            q.push(i);
        }
    }
    int row = 0;
    while (!q.empty()) {
        int size = q.size();
        for (int i = 0; i < size; i++) {
            int node = q.front();
            q.pop();
            processed++;
            position[node] = row++;  // Use row to assign the topological order position
            for (int neighbor : adj[node]) {
                indegree[neighbor]--;
                if (indegree[neighbor] == 0) {
                    q.push(neighbor);
                }
            }
        }
    }
    return processed == k;
}

vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {
    vector<vector<int>> adj1(k + 1), adj2(k + 1);
    vector<int> indegree1(k + 1, 0), indegree2(k + 1, 0);

    // Build the graph for row conditions
    for (const auto& condition : rowConditions) {
        int a = condition[0], b = condition[1];
        adj1[a].push_back(b);
        indegree1[b]++;
    }

    // Build the graph for column conditions
    for (const auto& condition : colConditions) {
        int a = condition[0], b = condition[1];
        adj2[a].push_back(b);
        indegree2[b]++;
    }

    map<int, int> rowPosition, colPosition;
    
    // Topological sort for row conditions
    if (!helper(adj1, indegree1, rowPosition)) {
        return {};  // Return empty if no valid topological order exists
    }
    
    // Topological sort for column conditions
    if (!helper(adj2, indegree2, colPosition)) {
        return {};  // Return empty if no valid topological order exists
    }

    // Build the result matrix
    vector<vector<int>> ans(k, vector<int>(k, 0));
    for (int i = 1; i <= k; i++) {
        int row = rowPosition[i];
        int col = colPosition[i];
        ans[row][col] = i;  // Place element `i` at the correct position
    }

    return ans;
}

};
60942