Problem p03690 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

typedef long long ll;

const int N = 4e5 + 50;

/*
int mul(int a,int b){ return 1ll*a*b%P; }
int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
*/

int n, a[N], b[N], p[N], c[N], f[N], ans, tot, d[N];
int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  // freopen("dat.in","r",stdin);
  // freopen("my.out","w",stdout);
#endif
  scanf("%d", &n);
  int s;
  s = 0;
  rep(i, 1, n) scanf("%d", a + i), s ^= a[i], c[i] = a[i];
  c[n + 1] = a[n + 1] = s;
  s = 0;
  rep(i, 1, n) scanf("%d", b + i), s ^= b[i], d[i] = b[i];
  d[n + 1] = b[n + 1] = s;
  ++n, sort(c + 1, c + n + 1), sort(d + 1, d + n + 1);
  rep(i, 1, n) if (c[i] != d[i]) {
    puts("-1");
    return 0;
  }
  rep(i, 1, n) if (a[i] != b[i]) {
    ans += i < n;
    if (!f[a[i]])
      f[a[i]] = ++tot;
    if (!f[b[i]])
      f[b[i]] = ++tot;
  }
  if (!ans) {
    puts("0");
    return 0;
  }
  if (!f[a[n]])
    f[a[n]] = ++tot;
  if (!f[b[n]])
    f[b[n]] = ++tot;
  rep(i, 1, tot) p[i] = i;
  rep(i, 1, n) if (a[i] != b[i]) p[find(f[a[i]])] = find(f[b[i]]);
  rep(i, 1, tot) ans += find(i) == i;
  printf("%d\n", --ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

typedef long long ll;

const int N = 4e5 + 50;

/*
int mul(int a,int b){ return 1ll*a*b%P; }
int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
*/

int n, a[N], b[N], p[N], c[N], ans, tot, d[N];
map<int, int> f;
int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  // freopen("dat.in","r",stdin);
  // freopen("my.out","w",stdout);
#endif
  scanf("%d", &n);
  int s;
  s = 0;
  rep(i, 1, n) scanf("%d", a + i), s ^= a[i], c[i] = a[i];
  c[n + 1] = a[n + 1] = s;
  s = 0;
  rep(i, 1, n) scanf("%d", b + i), s ^= b[i], d[i] = b[i];
  d[n + 1] = b[n + 1] = s;
  ++n, sort(c + 1, c + n + 1), sort(d + 1, d + n + 1);
  rep(i, 1, n) if (c[i] != d[i]) {
    puts("-1");
    return 0;
  }
  rep(i, 1, n) if (a[i] != b[i]) {
    ans += i < n;
    if (!f[a[i]])
      f[a[i]] = ++tot;
    if (!f[b[i]])
      f[b[i]] = ++tot;
  }
  if (!ans) {
    puts("0");
    return 0;
  }
  if (!f[a[n]])
    f[a[n]] = ++tot;
  if (!f[b[n]])
    f[b[n]] = ++tot;
  rep(i, 1, tot) p[i] = i;
  rep(i, 1, n) if (a[i] != b[i]) p[find(f[a[i]])] = find(f[b[i]]);
  rep(i, 1, tot) ans += find(i) == i;
  printf("%d\n", --ans);
  return 0;
}