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

/*
My intuition (WA) -
allowedSwaps will give connected components. We make DSU on indexes from allowedSwaps.

For an element from source s[i], we compare it with t[i] :
1. Equal => continue
2. Not equal and s[i] not in target => increase distance
3. Not equal and i and j are connected, where t[j]=s[i], then swappable.
4. Not equal and i and j are not connected => increase distance

One thing I missed is that there can be duplicate ele in array.

My intuition didn't work (gave WA).

From hints, I can traverse each component, and find common elements.

But if I have to perform dfs only, why need DSU?

*/
class Solution {
    void dfs(vector<vector<int>>& adj, vector<int>& source, vector<int>& target, int node, vector<bool>& visited, unordered_map<int,int>& hmap){
        visited[node]=true;
        hmap[source[node]]++;
        hmap[target[node]]--;
        for(auto child:adj[node]){
            if(!visited[child])
                dfs(adj,source,target,child,visited,hmap);
        }
    }
public:
    int minimumHammingDistance(vector<int>& source, vector<int>& target, vector<vector<int>>& allowedSwaps) {
        int n=source.size(), dist=0;
        vector<vector<int>> adj(n);
        for(auto &swp:allowedSwaps){
            int a=swp[0], b=swp[1];
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        vector<bool> visited(n,false);
        for(int i=0;i<n;i++){
            if(!visited[i]){
                unordered_map<int,int> hmap;
                dfs(adj,source,target,i,visited,hmap);
                for(auto &p:hmap)
                    dist += max(p.second,0);
            }
        }
        return dist;
    }
};
/*
class DSU {
    public:
    vector<int> parent,size;
    DSU(int n){
        parent.resize(n,-1);
        size.resize(n,1);
    }
    int Find(int v){
        if(parent[v]==-1)
            return v;
        return parent[v] = Find(parent[v]);
    }
    void Union(int a, int b){
        a=Find(a);
        b=Find(b);
        if(a==b) return;
        if(size[a]<size[b])
            swap(a,b);
        parent[b]=a;
        size[a]+=size[b];
    }
};
class Solution {
    void dfs(vector<vector<int>>& adj, vector<int>& source, vector<int>& target, int node, vector<bool>& visited, unordered_map<int,int>& hmap){
        visited[node]=true;
        hmap[source[node]]++;
        hmap[target[node]]--;
        for(auto child:adj[node]){
            if(!visited[child])
                dfs(adj,source,target,child,visited,hmap);
        }
    }
public:
    int minimumHammingDistance(vector<int>& source, vector<int>& target, vector<vector<int>>& allowedSwaps) {
        int n=source.size(), dist=0;
        vector<vector<int>> adj(n);
        DSU dsu(n);
        for(auto &swp:allowedSwaps){
            int a=swp[0], b=swp[1];
            dsu.Union(a,b);
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        for(int i=0;i<n;i++){
            if(dsu.parent[i]==-1){
                unordered_map<int,int> hmap;
                vector<bool> visited(n,false);
                dfs(adj,source,target,i,visited,hmap);
                for(auto &p:hmap)
                    dist += max(p.second,0);
            }
        }
        return dist;
    }
};
*/
175316