Problem p03690 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
template <typename T> inline int Chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline int Chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
inline int read() {
  int sum = 0, fl = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fl = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) + ch - '0';
  return sum * fl;
}

const int Maxn = 100000 + 100;

int N, A[Maxn], B[Maxn], A1[Maxn], B1[Maxn];
int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1];

inline void add_edge(int x, int y) {
  To[++e] = y;
  Next[e] = Begin[x];
  Begin[x] = e;
}

inline void Input() {
  N = read();
  int sum = 0;
  for (int i = 1; i <= N; ++i)
    A[i] = read(), sum ^= A[i];
  A[N + 1] = sum, sum = 0;
  for (int i = 1; i <= N; ++i)
    B[i] = read(), sum ^= B[i];
  B[++N] = sum;
}

int sum, Vis[Maxn], Viss[Maxn];

inline void dfs(int x) {
  ++sum;
  Vis[x] = 1;
  for (int i = Begin[x]; i; i = Next[i]) {
    int y = To[i];
    if (!Vis[y])
      dfs(y);
  }
}

inline void Solve() {
  for (int i = 1; i <= N; ++i)
    A1[i] = A[i], B1[i] = B[i];
  sort(A1 + 1, A1 + N + 1);
  sort(B1 + 1, B1 + N + 1);
  for (int i = 1; i <= N; ++i)
    if (A1[i] != B1[i]) {
      cout << -1 << endl;
      return;
    }
  int len = unique(A1 + 1, A1 + N + 1) - A1 - 1;
  for (int i = 1; i <= N; ++i)
    A[i] = lower_bound(A1 + 1, A1 + len + 1, A[i]) - A1;
  for (int i = 1; i <= N; ++i)
    B[i] = lower_bound(A1 + 1, A1 + len + 1, B[i]) - A1;

  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    if (A[i] != B[i] || i == N) {
      if (i != N)
        ++ans;
      add_edge(A[i], B[i]), add_edge(B[i], A[i]);
      Viss[A[i]] = Viss[B[i]] = 1;
    }
  }
  for (int i = 1; i <= N; ++i)
    if (!Vis[i] && Viss[i]) {
      dfs(i);
      ++ans;
    }

  cout << ans - 1 << endl;
}

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

Fixed submission:
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
template <typename T> inline int Chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline int Chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
inline int read() {
  int sum = 0, fl = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fl = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) + ch - '0';
  return sum * fl;
}

const int Maxn = 100000 + 100;

int N, A[Maxn], B[Maxn], A1[Maxn], B1[Maxn];
int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1];

inline void add_edge(int x, int y) {
  To[++e] = y;
  Next[e] = Begin[x];
  Begin[x] = e;
}

inline void Input() {
  N = read();
  int sum = 0;
  for (int i = 1; i <= N; ++i)
    A[i] = read(), sum ^= A[i];
  A[N + 1] = sum, sum = 0;
  for (int i = 1; i <= N; ++i)
    B[i] = read(), sum ^= B[i];
  B[++N] = sum;
}

int sum, Vis[Maxn], Viss[Maxn];

inline void dfs(int x) {
  ++sum;
  Vis[x] = 1;
  for (int i = Begin[x]; i; i = Next[i]) {
    int y = To[i];
    if (!Vis[y])
      dfs(y);
  }
}

inline void Solve() {
  for (int i = 1; i <= N; ++i)
    A1[i] = A[i], B1[i] = B[i];
  sort(A1 + 1, A1 + N + 1);
  sort(B1 + 1, B1 + N + 1);
  for (int i = 1; i <= N; ++i)
    if (A1[i] != B1[i]) {
      cout << -1 << endl;
      return;
    }
  int len = unique(A1 + 1, A1 + N + 1) - A1 - 1;
  for (int i = 1; i <= N; ++i)
    A[i] = lower_bound(A1 + 1, A1 + len + 1, A[i]) - A1;
  for (int i = 1; i <= N; ++i)
    B[i] = lower_bound(A1 + 1, A1 + len + 1, B[i]) - A1;

  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    if (A[i] != B[i] || i == N) {
      if (i != N)
        ++ans;
      add_edge(A[i], B[i]), add_edge(B[i], A[i]);
      Viss[A[i]] = Viss[B[i]] = 1;
    }
  }
  for (int i = 1; i <= N; ++i)
    if (!Vis[i] && Viss[i]) {
      dfs(i);
      ++ans;
    }

  cout << ans - 1 << endl;
}

int main() {
  Input();
  Solve();
  return 0;
}