<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 {
private:
    static bool topo(const uint16_t k, const vector<vector<int>> &conds, uint16_t sorted[k]) {
        uint16_t deg[k];
        fill(deg, deg + k, 0);
        const uint8_t qk = (k + 63u) >> 6;
        uint64_t adj[k][qk];
        fill(&**adj, &**adj + k * qk, 0);
        for (auto &e : conds) {
            const uint16_t w = e[1] - 1u;
            const uint8_t wb = w & 63u;
            uint64_t &aq = adj[e[0]-1u][w >> 6];
            deg[w] += ((aq >> wb) & 1u) ^ 1u;
            aq |= 1ull << wb;
        }

        uint16_t q[k], qstart = 0, qend = 0;
        for (uint16_t i = 0; i < k; i++) {
            q[qend] = i;
            qend += !deg[i];
        }

        uint l = 0;
        while (qstart != qend) {
            const uint16_t j = q[qstart++];
            if (qstart >= k) qstart = 0;
            sorted[l++] = j;
            for (uint16_t qi = 0; qi < qk; qi++)
                if (uint64_t aq = adj[j][qi]; aq) {
                    const uint16_t qb = qi << 6;
                    while (aq) {
                        const uint8_t bi = __builtin_ctzll(aq);
                        const uint16_t i = qb + bi;
                        if (!--deg[i]) {
                            q[qend++] = i;
                            if (qend >= k) qend = 0;
                        }
                        aq &= ~(1ull << bi);
                    }
                }
        }
        return l == k;
    }

public:
    static vector<vector<int>> buildMatrix(const uint16_t k, vector<vector<int>>& rconds, const vector<vector<int>>& cconds) {
        uint16_t rord[k], cord[k];
        if (!topo(k, rconds, rord) || !topo(k, cconds, cord))
            return {};

        vector<vector<int>> &res = rconds;
        const uint16_t rn = min<uint16_t>(res.size(), k);
        for (uint16_t i = 0; i < rn; i++)
            res[i].assign(k, 0);
        res.resize(k, vector<int>(k, 0));
        uint16_t rp[k], cp[k];
        fill(rp, rp + k, 0);
        fill(cp, cp + k, 0);
        for (uint16_t i = 0; i < k; i++)
            rp[rord[i]] = cp[cord[i]] = i;
        for (uint16_t i = 0; i < k; i++)
            res[rp[i]][cp[i]] = i + 1u;

        return move(res);
    }
};

auto init = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 'c';
}();
56982