<p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>

<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where each <code>adjacentPairs[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that the elements <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> are adjacent in <code>nums</code>.</p>

<p>It is guaranteed that every adjacent pair of elements <code>nums[i]</code> and <code>nums[i+1]</code> will exist in <code>adjacentPairs</code>, either as <code>[nums[i], nums[i+1]]</code> or <code>[nums[i+1], nums[i]]</code>. The pairs can appear <strong>in any order</strong>.</p>

<p>Return <em>the original array </em><code>nums</code><em>. If there are multiple solutions, return <strong>any of them</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> adjacentPairs = [[2,1],[3,4],[3,2]]
<strong>Output:</strong> [1,2,3,4]
<strong>Explanation:</strong> This array has all its adjacent pairs in adjacentPairs.
Notice that adjacentPairs[i] may not be in left-to-right order.
</pre>

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

<pre>
<strong>Input:</strong> adjacentPairs = [[4,-2],[1,4],[-3,1]]
<strong>Output:</strong> [-2,4,1,-3]
<strong>Explanation:</strong> There can be negative numbers.
Another solution is [-3,1,4,-2], which would also be accepted.
</pre>

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

<pre>
<strong>Input:</strong> adjacentPairs = [[100000,-100000]]
<strong>Output:</strong> [100000,-100000]
</pre>

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

<ul>
	<li><code>nums.length == n</code></li>
	<li><code>adjacentPairs.length == n - 1</code></li>
	<li><code>adjacentPairs[i].length == 2</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>5</sup> &lt;= nums[i], u<sub>i</sub>, v<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
	<li>There exists some <code>nums</code> that has <code>adjacentPairs</code> as its pairs.</li>
</ul>

class Solution {
private:
    static constexpr int MINV = -100000, MAXV = 100000;
    static constexpr unsigned RANGEV = MAXV - MINV + 1u;
    typedef unsigned long long u64;
    static u64 index[RANGEV];

    static unsigned lo(const u64 p) __attribute__((always_inline)) {
        return p & UINT_MAX;
    }

    static unsigned hi(const u64 p) __attribute__((always_inline)) {
        return p >> 32;
    }

    static void addp(u64 &p, const unsigned i) __attribute__((always_inline)) {
        p += (u64)i << ((p != 0) << 5);
    }

    static void removep(u64 &p, const unsigned i) __attribute__((always_inline)) {
        if (lo(p) == i) p = hi(p);
        else if (hi(p) == i) p = lo(p);
    }

public:
    vector<int> restoreArray(const vector<vector<int>>& apairs) {
        const unsigned n = apairs.size();
        if (n == 1) return apairs.front();
        int minv = MAXV, maxv = MINV;
        for (unsigned i = 0; i < n; i++) {
            const auto &ap = apairs[i];
            const int v0 = ap[0], v1 = ap[1];
            addp(index[v0-MINV], i + 1);
            addp(index[v1-MINV], i + 1);
            const auto [mn, mx] = minmax(v0, v1);
            minv = min(mn, minv);
            maxv = max(mx, maxv);
        }

        int head = minv;
        while (head <= maxv && index[head-MINV] - 1u >= UINT_MAX)
            head++;

        vector<int> r;
        r.reserve(n + 1);
        r.push_back(head);
        for (u64 idx = index[head-MINV]; idx; idx = index[head-MINV]) {
            const auto &ap = apairs[lo(idx) - 1];
            index[head-MINV] >>= 32;
            r.push_back(head = ap[ap[0] == head]);
            removep(index[head-MINV], lo(idx));
        }
        fill(index - MINV + minv, index - MINV + maxv + 1, 0u);
        return r;
    }
};

Solution::u64 Solution::index[RANGEV];
74208