Problem p03690 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;

queue<int> Q;
int a[N], b[N], Va[N], Vb[N], fa[N], vis[N];
int e;

inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
  return x * flag;
}

inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

int main() {
#ifndef ONLINE_JUDGE
  freopen("duliu.in", "r", stdin);
  freopen("duliu.out", "w", stdout);
#endif

  int n = read() + 1;
  For(i, 1, n - 1) a[i] = read(), a[n] ^= a[i];
  For(i, 1, n - 1) b[i] = read(), b[n] ^= b[i];

  For(i, 1, n) Va[i] = a[i], Vb[i] = b[i];
  sort(Va + 1, Va + n + 1);
  sort(Vb + 1, Vb + n + 1);
  For(i, 1, n) if (Va[i] != Vb[i]) return puts("-1"), 0;
  int cnt = unique(Va + 1, Va + n + 1) - Va - 1;
  For(i, 1, n) fa[i] = i;
  For(i, 1, n) {
    a[i] = lower_bound(Va + 1, Va + cnt + 1, a[i]) - Va;
    b[i] = lower_bound(Va + 1, Va + cnt + 1, b[i]) - Va;
    int x = find(a[i]), y = find(b[i]);
    if (x == y)
      continue;
    fa[x] = y;
  }

  int ans = 0, sum = 0;
  For(i, 1, n - 1) if (a[i] != b[i]) {
    ans++;
    vis[a[i]] = vis[b[i]] = true;
  }
  vis[a[n]] = vis[b[n]] = true;
  For(i, 1, cnt) if (vis[i] && find(i) == i) sum++;

  printf("%d\n", ans + sum - 1);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;

queue<int> Q;
int a[N], b[N], Va[N], Vb[N], fa[N], vis[N];
int e;

inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
  return x * flag;
}

inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

int main() {
  int n = read() + 1;
  For(i, 1, n - 1) a[i] = read(), a[n] ^= a[i];
  For(i, 1, n - 1) b[i] = read(), b[n] ^= b[i];

  For(i, 1, n) Va[i] = a[i], Vb[i] = b[i];
  sort(Va + 1, Va + n + 1);
  sort(Vb + 1, Vb + n + 1);
  For(i, 1, n) if (Va[i] != Vb[i]) return puts("-1"), 0;
  int cnt = unique(Va + 1, Va + n + 1) - Va - 1;
  For(i, 1, n) fa[i] = i;
  For(i, 1, n) {
    a[i] = lower_bound(Va + 1, Va + cnt + 1, a[i]) - Va;
    b[i] = lower_bound(Va + 1, Va + cnt + 1, b[i]) - Va;
    int x = find(a[i]), y = find(b[i]);
    if (x == y)
      continue;
    fa[x] = y;
  }

  int ans = 0, sum = 0;
  For(i, 1, n - 1) if (a[i] != b[i]) {
    ans++;
    vis[a[i]] = vis[b[i]] = true;
  }
  vis[a[n]] = vis[b[n]] = true;
  For(i, 1, cnt) if (vis[i] && find(i) == i) sum++;

  printf("%d\n", ans + sum - 1);

  return 0;
}