Problem p00602 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

const int MOD = 1001;

int fib[1002];

class UnionFind {
public:
  UnionFind() {}
  UnionFind(int n) { init(n); }
  void init(int n) {
    REP(i, n) {
      parent[i] = i;
      rank[i] = 0;
    }
  }
  int find(int v) {
    if (v == parent[v]) {
      return v;
    }
    return parent[v] = find(parent[v]);
  }
  void unite(int x, int y) {
    if (rank[x] > rank[y]) {
      parent[y] = x;
    } else {
      parent[x] = y;
      if (rank[x] == rank[y]) {
        rank[y] += 1;
      }
    }
  }
  bool same(int u, int v) { return find(u) == find(v); }

private:
  int parent[1002], rank[1002];
};

UnionFind uf;

int main() {
  fib[0] = 1;
  fib[1] = 2;
  FOR(i, 2, 1002) { fib[i] = (fib[i - 1] + fib[i - 2]) % MOD; }

  int V, d;
  while (std::cin >> V >> d, !std::cin.eof()) {
    uf.init(V + 1);
    FOR(i, 1, V + 1) {
      FOR(j, i + 1, V + 1) {
        if (std::abs(fib[i] - fib[j]) < d) {
          uf.unite(i, j);
        }
      }
    }

    int res = 0;
    FOR(i, 1, V + 1) {
      if (i == uf.find(i)) {
        res += 1;
      }
    }

    std::cout << res << std::endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

const int MOD = 1001;

int fib[1002];

class UnionFind {
public:
  UnionFind() {}
  UnionFind(int n) { init(n); }
  void init(int n) {
    REP(i, n) {
      parent[i] = i;
      rank[i] = 0;
    }
  }
  int find(int v) {
    if (v == parent[v]) {
      return v;
    }
    return parent[v] = find(parent[v]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return;
    }

    if (rank[x] > rank[y]) {
      parent[y] = x;
    } else {
      parent[x] = y;
      if (rank[x] == rank[y]) {
        rank[y] += 1;
      }
    }
  }
  bool same(int u, int v) { return find(u) == find(v); }

private:
  int parent[1002], rank[1002];
};

UnionFind uf;

int main() {
  fib[0] = 1;
  fib[1] = 2;
  FOR(i, 2, 1002) { fib[i] = (fib[i - 1] + fib[i - 2]) % MOD; }

  int V, d;
  while (std::cin >> V >> d, !std::cin.eof()) {
    uf.init(V + 1);
    FOR(i, 1, V + 1) {
      FOR(j, i + 1, V + 1) {
        if (std::abs(fib[i] - fib[j]) < d) {
          uf.unite(i, j);
        }
      }
    }

    int res = 0;
    FOR(i, 1, V + 1) {
      if (i == uf.find(i)) {
        res += 1;
      }
    }

    std::cout << res << std::endl;
  }
}