<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>

using ll = long long;
using pl = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vll = vector<pair<ll,ll>>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

void read(vl& a){for(auto &x : a) cin >> x;}
void read(vll& a){for(auto &x : a) cin >> x.first >> x.second;}
void read(vvl& a){for(auto &i : a) for(auto &x : i) cin >> x;}
void print(ll a){ cout<<a<<"\n";}
void print(string s){cout<<s<<"\n";}
void print(pl a){ cout<<a.first<<" "<<a.second<<" ";}
void print(vl a){for(auto x : a) cout<<x<<" ";cout<<"\n";}
void print(vll a){for(auto x : a) cout<<x.first<<" "<<x.second<<"\n";}
void print(vvl a){for(auto &&x : a){for(auto &&i : x)cout<<i<<" ";cout<<"\n";}}



class Solution {
public:
   
   
ll active_node = 1; 
ll expired_node = 2; 

    bool f(ll cur,vl &vis,stack<ll> &st,vvl &g){

        if(vis[cur]==active_node){
            print("Cycle detected !!! ");
            return false;
        }
        if(vis[cur]==expired_node)return true;
        
        vis[cur] = active_node;
       
        for(auto &&child : g[cur]){
           if(!f(child,vis,st,g)){
              return false;
           } 
        }
        
        st.push(cur);
        vis[cur] = expired_node;

        return true;

    }


    vector<vector<int>> buildMatrix(int k, vector<vector<int>> r, vector<vector<int>> c) {


        vvl adj(k+1);vl vis(k+1,0ll);vl ind(k+1,0ll);vector<vector<int>> ans;
        for(ll i=0;i<r.size();i++)adj[r[i][0]].push_back(r[i][1]),ind[r[i][1]]++;

        ll id = -1; for(ll i=1;i<=k;i++){if(!ind[i]){id = i;break;}}
        stack<ll> st;if(id==-1 ){return ans;}

        for(ll i=1;i<=k;i++){
            if(ind[i]==0 and vis[i]==0){
                if(!f(i,vis,st,adj)){return ans;}
            }
        }
        
        vl x;while(!st.empty())x.push_back(st.top()),st.pop();
        for(ll i=1;i<=k;i++)adj[i].clear(),ind[i]=0,vis[i]=0;id = -1;
        for(ll i=0;i<c.size();i++)adj[c[i][0]].push_back(c[i][1]),ind[c[i][1]]++;

        for(ll i=1;i<=k;i++){if(!ind[i]){id = i;break;}}
        if(id==-1){return ans;}
        for(ll i=1;i<=k;i++){
            if(ind[i]==0 and vis[i]==0){
                if(!f(i,vis,st,adj)){return ans;}
            }
        }
        vl y;while(!st.empty())y.push_back(st.top()),st.pop();

        print(x);
        print(y);

        if(!(x.size()==k and y.size()==k)){
            return ans;
        }

        vl r_(k+1),c_(k+1);ans.resize(k,vector<int>(k,0));
        for(ll i=0;i<k;i++)r_[x[i]] = i,c_[y[i]] = i; 

        for(ll i=1;i<=k;i++){
            ans[r_[i]][c_[i]] = i;
        }

        return ans;

        
    }
};
68697