Problem p03690 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int INF = 1001001001;
const int INFLL = 1001001001001001001ll;
const int mod = 1000000007;
inline void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

struct UnionFindTree {
  vector<int> par, sz;
  UnionFindTree(int n) {
    par.resize(n);
    sz.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] < sz[y])
      swap(x, y);
    sz[x] += sz[y];
    par[y] = x;
  }
  bool areSame(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};

int calc(vint A, vint B) {
  if (A.size() == 0)
    return 0;
  vint xs;
  rep(i, A.size()) xs.pb(A[i]);
  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());

  rep(i, A.size()) {
    A[i] = lower_bound(all(xs), A[i]) - xs.begin();
    B[i] = lower_bound(all(xs), B[i]) - xs.begin();
  }

  UnionFindTree uf(xs.size());
  rep(i, A.size()) uf.unite(A[i], B[i]);

  set<int> s;
  rep(i, A.size()) s.insert(uf.find(i));
  return A.size() + s.size();
}

signed main() {
  int N;
  cin >> N;
  vint A(N), B(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];

  int a = 0;
  rep(i, N) a ^= A[i];
  int b = 0;
  rep(i, N) b ^= B[i];

  A.pb(a);
  B.pb(b);

  vint latte = A, malta = B;
  sort(all(latte));
  sort(all(malta));

  rep(i, N + 1) if (latte[i] != malta[i]) {
    puts("-1");
    return 0;
  }

  vint AA, BB;
  int beet = 0;
  rep(i, N + 1) if (A[i] != B[i]) {
    AA.pb(A[i]);
    BB.pb(B[i]);
    if (A[i] == B[N])
      beet = 2;
  }

  if (beet == 2 && A[N] == B[N])
    beet--;

  printf("%lld\n", calc(AA, BB) - beet);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int INF = 1001001001;
const int INFLL = 1001001001001001001ll;
const int mod = 1000000007;
inline void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}

struct UnionFindTree {
  vector<int> par, sz;
  UnionFindTree(int n) {
    par.resize(n);
    sz.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] < sz[y])
      swap(x, y);
    sz[x] += sz[y];
    par[y] = x;
  }
  bool areSame(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};

int calc(vint A, vint B) {
  if (A.size() == 0)
    return 0;
  vint xs;
  rep(i, A.size()) xs.pb(A[i]);
  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());

  rep(i, A.size()) {
    A[i] = lower_bound(all(xs), A[i]) - xs.begin();
    B[i] = lower_bound(all(xs), B[i]) - xs.begin();
  }

  UnionFindTree uf(xs.size());
  rep(i, A.size()) uf.unite(A[i], B[i]);

  set<int> s;
  rep(i, A.size()) s.insert(uf.find(A[i]));
  return A.size() + s.size();
}

signed main() {
  int N;
  cin >> N;
  vint A(N), B(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];

  int a = 0;
  rep(i, N) a ^= A[i];
  int b = 0;
  rep(i, N) b ^= B[i];

  A.pb(a);
  B.pb(b);

  vint latte = A, malta = B;
  sort(all(latte));
  sort(all(malta));

  rep(i, N + 1) if (latte[i] != malta[i]) {
    puts("-1");
    return 0;
  }

  vint AA, BB;
  int beet = 0;
  rep(i, N + 1) if (A[i] != B[i]) {
    AA.pb(A[i]);
    BB.pb(B[i]);
    if (A[i] == B[N])
      beet = 2;
  }

  if (beet == 2 && A[N] == B[N])
    beet--;

  printf("%lld\n", calc(AA, BB) - beet);
  return 0;
}