inttrace(int from, vector<int>& source, vector<int>& target, vector<vector<int>>& graph, vector<bool>& visited){ int ans = 0; queue<int> q; vector<int> a; unordered_multiset<int> b;
put(from, source, target, visited, a, b, q); while (q.size()) { int from = q.front(); q.pop(); for (int to : graph[from]) { if (!visited[to]) { put(to, source, target, visited, a, b, q); } } }
for (int t : a) { unordered_multiset<int>::iterator it = b.find(t); if (it == b.end()) { ans++; } else { b.erase(it); } } return ans; } public: intminimumHammingDistance(vector<int>& source, vector<int>& target, vector<vector<int>>& allowedSwaps){ int n = source.size(); vector<vector<int>> graph(n); for (vector<int>& s : allowedSwaps) { graph[s[0]].push_back(s[1]); graph[s[1]].push_back(s[0]); } int ans = 0; vector<bool> visited(n); for (int i = 0; i < n; i++) { if (!visited[i]) { ans += trace(i, source, target, graph, visited); } } return ans; } };
inttrace(int from, vector<int>& source, vector<int>& target, vector<vector<int>>& graph, vector<bool>& visited){ int ans = 0; queue<int> q; unordered_map<int, int> diff;
put(from, source, target, visited, diff, q); while (q.size()) { int from = q.front(); q.pop(); for (int to : graph[from]) { if (!visited[to]) { put(to, source, target, visited, diff, q); } } }
for (auto [_, d] : diff) { ans += abs(d); } return ans; } public: intminimumHammingDistance(vector<int>& source, vector<int>& target, vector<vector<int>>& allowedSwaps){ int n = source.size(); vector<vector<int>> graph(n); for (vector<int>& s : allowedSwaps) { graph[s[0]].push_back(s[1]); graph[s[1]].push_back(s[0]); } int ans = 0; vector<bool> visited(n); for (int i = 0; i < n; i++) { if (!visited[i]) { ans += trace(i, source, target, graph, visited); } } return ans / 2; } };