Problem p03690 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
struct UnionFind {
  vector<int> par, siz;
  UnionFind(int n) {
    par.resize(n);
    siz.resize(n, 1);
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  bool unite(int u, int v) {
    u = find(u), v = find(v);
    if (u == v)
      return false;
    if (siz[u] < siz[v])
      swap(u, v);
    par[v] = u;
    siz[u] += siz[v];
    return true;
  }
  bool same(int u, int v) { return find(u) == find(v); }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n + 1), b(n + 1);
  map<int, int> mp1, mp2;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[n] ^= a[i];
    mp1[a[i]]++;
  }
  mp1[a[n]]++;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[n] ^= b[i];
    mp2[b[i]]++;
  }
  mp2[b[n]]++;
  int id = 0;
  for (auto it = mp1.begin(); it != mp1.end(); it++) {
    if (it->second != mp2[it->first]) {
      cout << -1 << endl;
      return 0;
    }
    it->second = id;
    id++;
  }
  UnionFind uf(id);
  int res = 0;
  for (int i = 0; i <= n; i++) {
    if (a[i] != b[i]) {
      res++;
      uf.unite(mp1[a[i]], mp1[b[i]]);
    }
  }
  map<int, int> mp;
  for (int i = 0; i < id; i++) {
    mp[uf.find(i)]++;
  }
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it->second > 1 && !uf.same(it->first, a[n])) {
      res++;
    }
  }
  if (a[n] != b[n]) {
    res--;
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
struct UnionFind {
  vector<int> par, siz;
  UnionFind(int n) {
    par.resize(n);
    siz.resize(n, 1);
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  bool unite(int u, int v) {
    u = find(u), v = find(v);
    if (u == v)
      return false;
    if (siz[u] < siz[v])
      swap(u, v);
    par[v] = u;
    siz[u] += siz[v];
    return true;
  }
  bool same(int u, int v) { return find(u) == find(v); }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n + 1), b(n + 1);
  map<int, int> mp1, mp2;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[n] ^= a[i];
    mp1[a[i]]++;
  }
  mp1[a[n]]++;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[n] ^= b[i];
    mp2[b[i]]++;
  }
  mp2[b[n]]++;
  int id = 0;
  for (auto it = mp1.begin(); it != mp1.end(); it++) {
    if (it->second != mp2[it->first]) {
      cout << -1 << endl;
      return 0;
    }
    it->second = id;
    id++;
  }
  UnionFind uf(id);
  int res = 0;
  for (int i = 0; i <= n; i++) {
    if (a[i] != b[i]) {
      res++;
      uf.unite(mp1[a[i]], mp1[b[i]]);
    }
  }
  map<int, int> mp;
  for (int i = 0; i < id; i++) {
    mp[uf.find(i)]++;
  }
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it->second > 1 && !uf.same(it->first, mp1[a[n]])) {
      res++;
    }
  }
  if (a[n] != b[n]) {
    res--;
  }
  cout << res << endl;
}