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

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// #ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
// #endif

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#include <array>
// #include <atomic>
#include <chrono>
// #include <condition_variable>
#include <forward_list>
// #include <future>
#include <initializer_list>
// #include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
// #include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>

using namespace std;

template <typename T> class UF {
public:
  UF() { count = 0; }
  void push(T a) {
    M[a] = count;
    V.push_back(count);
    rank.push_back(0);
    count++;
  }

  void unite(T a, T b) {
    long long int x = find(M[a]);
    long long int y = find(M[b]);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      V[x] = y;
    } else {
      V[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  void merge(T a, T b) { unite(a, b); }
  bool same(T a, T b) { return find(M[a]) == find(M[b]); }
  long long int group() {
    set<long long int> S;
    for (long long int i = 0; i < V.size(); i++) {
      S.insert(find(V[i]));
    }
    return S.size();
  }

private:
  vector<long long int> V, rank;
  map<T, long long int> M;
  long long int count;

  long long int find(long long int x) {
    if (V[x] == x)
      return x;
    else
      return V[x] = find(V[x]);
  }
};

int main() {
  int V, d;
  int data[1001] = {};
  data[0] = 2;
  data[1] = 3;
  for (size_t i = 2; i < 1001; i++) {
    data[i] = data[i - 1] + data[i - 2];
    data[i] %= 1001;
  }

  while (cin >> V >> d) {
    UF<int> *uf = new UF<int>;
    set<int> S;
    for (size_t i = 0; i < V; i++) {
      S.insert(data[i]);
    }
    for (auto x : S) {
      (*uf).push(x);
    }
    for (size_t i = 0; i < V; i++) {
      for (size_t j = i + 1; j < V; j++) {
        if (abs(data[i] - data[j]) < d) {
          (*uf).merge(data[i], data[j]);
        }
      }
    }
    cout << (*uf).group() << endl;
    delete uf;
  }
}

Error: -11

Fixed submission:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// #ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
// #endif

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#include <array>
// #include <atomic>
#include <chrono>
// #include <condition_variable>
#include <forward_list>
// #include <future>
#include <initializer_list>
// #include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
// #include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>

using namespace std;

template <typename T> class UF {
public:
  void push(T a) {
    static long long int count = 0;
    M[a] = count;
    V.push_back(count);
    rank.push_back(0);
    count++;
  }

  void unite(T a, T b) {
    long long int x = find(M[a]);
    long long int y = find(M[b]);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      V[x] = y;
    } else {
      V[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  void merge(T a, T b) { unite(a, b); }
  bool same(T a, T b) { return find(M[a]) == find(M[b]); }
  long long int group() {
    set<long long int> S;
    for (long long int i = 0; i < V.size(); i++) {
      S.insert(find(V[i]));
    }
    return S.size();
  }

private:
  vector<long long int> V, rank;
  map<T, long long int> M;
  // long long int count;

  long long int find(long long int x) {
    if (V[x] == x)
      return x;
    else
      return V[x] = find(V[x]);
  }
};

int main() {
  int V, d;
  int data[1001] = {};
  data[0] = 2;
  data[1] = 3;
  for (size_t i = 2; i < 1001; i++) {
    data[i] = data[i - 1] + data[i - 2];
    data[i] %= 1001;
  }

  while (cin >> V >> d) {
    UF<int> *uf = new UF<int>;
    for (size_t i = 0; i < V; i++) {
      (*uf).push(data[i]);
    }
    for (size_t i = 0; i < V; i++) {
      for (size_t j = i + 1; j < V; j++) {
        if (abs(data[i] - data[j]) < d) {
          (*uf).merge(data[i], data[j]);
        }
      }
    }
    cout << (*uf).group() << endl;
    delete uf;
  }
}