Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP1(i, n) for (int i = 1; i < (n); i++)
#define REP2(i, d, n) for (int i = (d); i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define CLR(a) memset((a), 0, sizeof(a))
#define MCLR(a) memset((a), -1, sizeof(a))
#define RANGE(x, y, maxX, maxY)                                                \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

struct UnionFind {
  vector<LL> data;
  UnionFind(LL size) : data(size, -1) {}
  bool unionSet(LL x, LL y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(LL x, LL y) { return root(x) == root(y); }
  LL root(LL x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  LL size(LL x) { return -data[root(x)]; }
};

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  LL ans = 1;
  REP1(i, N) ans += i;
  vector<LL> result;

  UnionFind uf(N);
  RREP(i, A.size() - 1) {
    result.PB(ans);

    if (!uf.findSet(A[i], B[i])) {
      LL a_size = uf.size(A[i]);
      LL b_size = uf.size(B[i]);
      uf.unionSet(A[i], B[i]);
      ans -= a_size * b_size;
    }
  }

  RREP(i, result.size() - 1) { cout << result[i] - 1 << endl; }
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP1(i, n) for (int i = 1; i < (n); i++)
#define REP2(i, d, n) for (int i = (d); i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define CLR(a) memset((a), 0, sizeof(a))
#define MCLR(a) memset((a), -1, sizeof(a))
#define RANGE(x, y, maxX, maxY)                                                \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

struct UnionFind {
  vector<LL> data;
  UnionFind(LL size) : data(size, -1) {}
  bool unionSet(LL x, LL y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(LL x, LL y) { return root(x) == root(y); }
  LL root(LL x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  LL size(LL x) { return -data[root(x)]; }
};

void solve(long long N, long long M, std::vector<long long> A,
           std::vector<long long> B) {
  LL ans = 1;
  REP1(i, N) ans += i;
  vector<LL> result;
  REP(i, M) {
    A[i]--;
    B[i]--;
  }

  UnionFind uf(N);
  RREP(i, A.size() - 1) {
    result.PB(ans);

    if (!uf.findSet(A[i], B[i])) {
      LL a_size = uf.size(A[i]);
      LL b_size = uf.size(B[i]);
      uf.unionSet(A[i], B[i]);
      ans -= a_size * b_size;
    }
  }

  RREP(i, result.size() - 1) { cout << result[i] - 1 << endl; }
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}