<p>You are given two integer arrays, <code>source</code> and <code>target</code>, both of length <code>n</code>. You are also given an array <code>allowedSwaps</code> where each <code>allowedSwaps[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you are allowed to swap the elements at index <code>a<sub>i</sub></code> and index <code>b<sub>i</sub></code> <strong>(0-indexed)</strong> of array <code>source</code>. Note that you can swap elements at a specific pair of indices <strong>multiple</strong> times and in <strong>any</strong> order.</p>

<p>The <strong>Hamming distance</strong> of two arrays of the same length, <code>source</code> and <code>target</code>, is the number of positions where the elements are different. Formally, it is the number of indices <code>i</code> for <code>0 &lt;= i &lt;= n-1</code> where <code>source[i] != target[i]</code> <strong>(0-indexed)</strong>.</p>

<p>Return <em>the <strong>minimum Hamming distance</strong> of </em><code>source</code><em> and </em><code>target</code><em> after performing <strong>any</strong> amount of swap operations on array </em><code>source</code><em>.</em></p>

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

<pre>
<strong>Input:</strong> source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> source can be transformed the following way:
- Swap indices 0 and 1: source = [<u>2</u>,<u>1</u>,3,4]
- Swap indices 2 and 3: source = [2,1,<u>4</u>,<u>3</u>]
The Hamming distance of source and target is 1 as they differ in 1 position: index 3.
</pre>

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

<pre>
<strong>Input:</strong> source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = []
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are no allowed swaps.
The Hamming distance of source and target is 2 as they differ in 2 positions: index 1 and index 2.
</pre>

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

<pre>
<strong>Input:</strong> source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]]
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>n == source.length == target.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= source[i], target[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= allowedSwaps.length &lt;= 10<sup>5</sup></code></li>
	<li><code>allowedSwaps[i].length == 2</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n - 1</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>

class Solution {
public:
    int find(vector<int> &ds, int i) {
        // this is to keep track of rank (number of elements in the group).
        // Note that root element of the group is indicated by a negative value. Both i and j here are root elements of two groups that we merge.
        return ds[i] < 0 ? i : ds[i] = find(ds, ds[i]);
    }
    
    int minimumHammingDistance(vector<int>& s, vector<int>& t, vector<vector<int>>& allowedSwaps) {
        int sz = s.size(), res = 0;
        vector<int> ds(sz, -1);
        
        for (auto &sw: allowedSwaps) {
            int i = find(ds, sw[0]), j = find(ds, sw[1]);
            // cout << i << "~~" << j << endl;
            // cout << ds[i] << "::" << ds[j] << endl;
            if (i != j) {
                // if (ds[i] > ds[j])
                //     swap(i, j);
                // // ONLY source island has weights?
                // seems just counting (negative) how many elements there are in the current union. i and j are both roots. It's not necessary for this problem but I think it's a useful trick.
                ds[i] += ds[j];
                ds[j] = i;
            // cout << ds[i] << ">>" << ds[j] << endl;
            }
        }
        
        // for(auto a: ds) cout << a << "%%" << endl;
        
        // std::unordered_multiset is an associative container that contains set of possibly non-unique objects of type Key. Search, insertion, and removal have average constant-time complexity.
        vector<unordered_multiset<int>> cycles(sz);
        
        for (auto i = 0; i < sz; ++i)
            if (s[i] != t[i])   cycles[find(ds, i)].insert(s[i]);
        
        for (auto i = 0; i < sz; ++i) {
            if (s[i] != t[i]) {
                auto &cycle = cycles[find(ds, i)];
                auto it = cycle.find(t[i]);
                
                if (it != end(cycle)) {
                    // Erasing by address erases the value at that address while erasing using the value will erase all the occurrences of that value.
                    // remember, multiset can have duplicate items, so we erase it here
                    cycle.erase(it);
                } else
                    ++res;
            }
        }
        return res;
    }
};
137449