<p>There are <code>n</code> couples sitting in <code>2n</code> seats arranged in a row and want to hold hands.</p>

<p>The people and seats are represented by an integer array <code>row</code> where <code>row[i]</code> is the ID of the person sitting in the <code>i<sup>th</sup></code> seat. The couples are numbered in order, the first couple being <code>(0, 1)</code>, the second couple being <code>(2, 3)</code>, and so on with the last couple being <code>(2n - 2, 2n - 1)</code>.</p>

<p>Return <em>the minimum number of swaps so that every couple is sitting side by side</em>. A swap consists of choosing any two people, then they stand up and switch seats.</p>

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

<pre>
<strong>Input:</strong> row = [0,2,1,3]
<strong>Output:</strong> 1
<strong>Explanation:</strong> We only need to swap the second (row[1]) and third (row[2]) person.
</pre>

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

<pre>
<strong>Input:</strong> row = [3,2,0,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All couples are already seated side by side.
</pre>

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

<ul>
	<li><code>2n == row.length</code></li>
	<li><code>2 &lt;= n &lt;= 30</code></li>
	<li><code>n</code> is even.</li>
	<li><code>0 &lt;= row[i] &lt; 2n</code></li>
	<li>All the elements of <code>row</code> are <strong>unique</strong>.</li>
</ul>

class DSU {
    vector<int> rank, parent;
public:
    DSU(int n) {
        rank.resize(n+1, 0);
        parent.resize(n+1);
        for (int i = 0; i <= n; i++) {
            parent[i] = i;
        }
    }

    int findUPar(int node) {
        if (node == parent[node])
            return node;
        return parent[node] = findUPar(parent[node]);
    }

    void unionByRank(int u, int v) {
        int ulp_u = findUPar(u);
        int ulp_v = findUPar(v);
        if (ulp_u == ulp_v) return;
        if (rank[ulp_u] < rank[ulp_v]) {
            parent[ulp_u] = ulp_v;
        }
        else if (rank[ulp_v] < rank[ulp_u]) {
            parent[ulp_v] = ulp_u;
        }
        else {
            parent[ulp_v] = ulp_u;
            rank[ulp_u]++;
        }
    }
};

class Solution {
public:
    int minSwapsCouples(vector<int>& row) {
        map<int,int> mpp;
        int n = row.size();
        for(int i = 0 ; i < n ; i ++){
            mpp[row[i]] = i;
        }
        DSU ds(n);
        int ans = 0;
        for(int i = 0 ; i < n ; i ++){
            if(row[i] % 2 == 0){
                int partners_idx = mpp[row[i]+1];
                int curr_node = (i%2==0)? i : i-1;
                int part_node = (partners_idx%2==0)? partners_idx : partners_idx-1;
                if(ds.findUPar(curr_node) != ds.findUPar(part_node)){
                    ds.unionByRank(curr_node, part_node);
                    ans++;
                }                
            }
            else{
                int partners_idx = mpp[row[i]-1];
                int curr_node = (i%2==0)? i : i-1;
                int part_node = (partners_idx%2==0)? partners_idx : partners_idx-1;
                if(ds.findUPar(curr_node) != ds.findUPar(part_node)){
                    ds.unionByRank(curr_node, part_node);
                    ans ++;
                }
            }
        }
        return ans;
    }
};
10300