Problem p03108 (C++)

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

int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }

#define CIN(T, x)                                                              \
  T x;                                                                         \
  cin >> x;
#define CINS(T, x, N)                                                          \
  vector<T> x(N);                                                              \
  for (int i = 0; i < (N); i++) {                                              \
    cin >> x[i];                                                               \
  }
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << " ";                                                       \
  }                                                                            \
  cout << "\n";
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << "\n";

int N, M;
// int N_MAX = 1e5+5;
int N_MAX = 30;
vector<int> parent(N_MAX);
vector<ll> treeSize(N_MAX, 1);

int root(int x) {
  if (x == parent[x])
    return x;
  parent[x] = root(parent[x]);
  return parent[x];
}

void unite(int x, int y) {
  int rx = root(x);
  int ry = root(y);
  if (rx == ry)
    return;

  if (treeSize[rx] >= treeSize[ry]) {
    parent[ry] = rx;
    treeSize[rx] += treeSize[ry];
    treeSize[ry] = 0;
  } else {
    parent[rx] = ry;
    treeSize[ry] += treeSize[rx];
    treeSize[rx] = 0;
  }
}

bool same(int x, int y) { return root(x) == root(y); }

int main() {

  cin >> N >> M;
  vector<int> A(M);
  vector<int> B(M);

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  vector<ll> ans(M);
  ans[M - 1] = N * (N - 1) / 2;

  for (int i = 0; i < N; i++) {
    parent[i] = i;
  }

  for (int i = M - 1; i > 0; i--) {

    if (same(A[i], B[i])) {
      ans[i - 1] = ans[i];
    } else {
      ans[i - 1] = ans[i] - treeSize[parent[A[i]]] * treeSize[parent[B[i]]];
      unite(A[i], B[i]);
    }
  }

  for (int i = 0; i < M; i++) {
    cout << ans[i] << "\n";
  }

  return 0;
}

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

int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }

#define CIN(T, x)                                                              \
  T x;                                                                         \
  cin >> x;
#define CINS(T, x, N)                                                          \
  vector<T> x(N);                                                              \
  for (int i = 0; i < (N); i++) {                                              \
    cin >> x[i];                                                               \
  }
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << " ";                                                       \
  }                                                                            \
  cout << "\n";
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << "\n";

ll N, M;
int N_MAX = 1e5 + 5;
// int N_MAX = 30;
vector<int> parent(N_MAX);
vector<ll> treeSize(N_MAX, 1);

int root(int x) {
  if (x == parent[x])
    return x;
  parent[x] = root(parent[x]);
  return parent[x];
}

void unite(int x, int y) {
  int rx = root(x);
  int ry = root(y);
  if (rx == ry)
    return;

  if (treeSize[rx] >= treeSize[ry]) {
    parent[ry] = rx;
    treeSize[rx] += treeSize[ry];
    treeSize[ry] = 0;
  } else {
    parent[rx] = ry;
    treeSize[ry] += treeSize[rx];
    treeSize[rx] = 0;
  }
}

bool same(int x, int y) { return root(x) == root(y); }

int main() {

  cin >> N >> M;
  vector<int> A(M);
  vector<int> B(M);

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  vector<ll> ans(M);
  ans[M - 1] = N * (N - 1) / 2;

  for (int i = 0; i < N; i++) {
    parent[i] = i;
  }

  for (int i = M - 1; i > 0; i--) {

    if (same(A[i], B[i])) {
      ans[i - 1] = ans[i];
    } else {
      ans[i - 1] = ans[i] - treeSize[parent[A[i]]] * treeSize[parent[B[i]]];
      unite(A[i], B[i]);
    }
  }

  for (int i = 0; i < M; i++) {
    cout << ans[i] << "\n";
  }

  return 0;
}