Problem p01334 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

namespace tree {
struct union_find {
  vector<int> par, rank, size;
  int compnum;

  union_find(int N) {
    compnum = N;
    par.resize(N), rank.resize(N), size.resize(N);
    for (int i = 0; i < N; i++) {
      par[i] = i;
      rank[i] = 0;
      size[i] = 1;
    }
  }

  int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }

  void unite(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y, size[y] += size[x];
    } else {
      par[y] = x, size[x] += size[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
    compnum--;
  }

  int operator[](int x) { return root(x); }
  void operator()(int x, int y) { return unite(x, y); }

  bool same(int x, int y) { return root(x) == root(y); }
  int size_of(int x) { return size[root(x)]; }
  int num_of_comps() { return compnum; }
};
} // namespace tree

int main() {

  for (int N; cin >> N && N;) {
    int toy[N][N], tox[N][N];
    rep(i, N) {
      rep(j, N) {
        int x, y;
        cin >> x >> y;
        toy[i][j] = y;
        tox[i][j] = x;
      }
    }

    tree::union_find uf(N * N);
    rep(_i, N) rep(_j, N) {
      int i = _i, j = _j;
      if ((i == toy[i][j] && j == tox[i][j]) ||
          !uf.same(i * N + j, toy[i][j] * N + tox[i][j])) {
        while (!uf.same(i * N + j, toy[i][j] * N + tox[i][j])) {
          uf(i * N + j, toy[i][j] * N + tox[i][j]);
          tie(i, j) = make_pair(toy[i][j], tox[i][j]);
        }
      }
    }
    cout << uf.num_of_comps() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

namespace tree {
struct union_find {
  static const int Max = 22;
  int par[Max], rank[Max], size[Max], compnum;

  union_find(int N) {
    compnum = N;
    for (int i = 0; i < N; i++) {
      par[i] = i;
      rank[i] = 0;
      size[i] = 1;
    }
  }

  int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }

  void unite(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y, size[y] += size[x];
    } else {
      par[y] = x, size[x] += size[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
    compnum--;
  }

  int operator[](int x) { return root(x); }
  void operator()(int x, int y) { return unite(x, y); }

  bool same(int x, int y) { return root(x) == root(y); }
  int size_of(int x) { return size[root(x)]; }
  int num_of_comps() { return compnum; }
};
} // namespace tree

int main() {

  for (int N; cin >> N && N;) {
    int toy[N][N], tox[N][N];
    rep(i, N) {
      rep(j, N) {
        int x, y;
        cin >> x >> y;
        toy[i][j] = y;
        tox[i][j] = x;
      }
    }

    tree::union_find uf(N * N);
    rep(_i, N) rep(_j, N) {
      int i = _i, j = _j;
      if ((i == toy[i][j] && j == tox[i][j]) ||
          !uf.same(i * N + j, toy[i][j] * N + tox[i][j])) {
        while (!uf.same(i * N + j, toy[i][j] * N + tox[i][j])) {
          uf(i * N + j, toy[i][j] * N + tox[i][j]);
          tie(i, j) = make_pair(toy[i][j], tox[i][j]);
        }
      }
    }
    cout << uf.num_of_comps() << endl;
  }

  return 0;
}