Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// clang-format off
#define FOR(i, n) for (int i=0; i<(int)n; ++i)
#define RFOR(i, n) for (int i=n-1; i>=0; --i)
#define OUTV(x) FOR(j,x.size()) cerr<<x[j]<<(x.size()-j-1?' ':'\n');
#define OUTM(x) FOR(i,x.size()) {OUTV(x[i])}
#define BLE cerr<<"BLE"<<endl;
#define DEBUG(x) cerr<<"DEBUG: "<<#x<<" = "<<x<<endl;
#define PB push_back
#define MP make_pair
// clang-format on
using namespace std;

int N, M;
vector<vector<int>> graph;
vector<int> p;
vector<long long> ans;
vector<int> component;

void merge(int a, int b) {
  if (p[a] > p[b])
    merge(b, a);
  if (p[a] == p[b])
    return;

  queue<int> Q;
  Q.push(b);

  component[p[a]] += component[p[b]];
  component[p[b]] = 0;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    p[u] = p[a];
    for (int v : graph[u]) {
      if (p[v] != p[a]) {
        Q.push(v);
      }
    }
  }
}

long long solve(long long res, int a, int b) {

  long long ret = res;
  if (p[a] != p[b])
    ret -= 1LL * component[p[a]] * component[p[b]];
  merge(a, b);
  //  OUTV(component);
  return ret;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  //  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin >> N >> M;
  graph.resize(N);
  p.resize(N);
  component.resize(N);
  for (int i = 0; i < N; ++i) {
    p[i] = i;
    component[i] = 1;
  }
  ans.resize(M + 1);
  ans[M] = (1LL * N * (N - 1)) / 2;
  vector<int> A(N), B(N);
  for (int i = 0; i < M; ++i) {
    cin >> A[i];
    A[i]--;
    cin >> B[i];
    B[i]--;
  }

  for (int i = 0; i < M; ++i) {
    graph[A[M - 1 - i]].push_back(B[M - 1 - i]);
    graph[B[M - 1 - i]].push_back(A[M - 1 - i]);
    ans[M - 1 - i] = solve(ans[M - i], A[M - 1 - i], B[M - 1 - i]);
  }
  for (int i = 1; i <= M; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// clang-format off
#define FOR(i, n) for (int i=0; i<(int)n; ++i)
#define RFOR(i, n) for (int i=n-1; i>=0; --i)
#define OUTV(x) FOR(j,x.size()) cerr<<x[j]<<(x.size()-j-1?' ':'\n');
#define OUTM(x) FOR(i,x.size()) {OUTV(x[i])}
#define BLE cerr<<"BLE"<<endl;
#define DEBUG(x) cerr<<"DEBUG: "<<#x<<" = "<<x<<endl;
#define PB push_back
#define MP make_pair
// clang-format on
using namespace std;

int N, M;
vector<vector<int>> graph;
vector<int> p;
vector<long long> ans;
vector<int> component;

void merge(int a, int b) {
  if (p[a] > p[b])
    merge(b, a);
  if (p[a] == p[b])
    return;

  queue<int> Q;
  Q.push(b);

  component[p[a]] += component[p[b]];
  component[p[b]] = 0;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    p[u] = p[a];
    for (int v : graph[u]) {
      if (p[v] != p[a]) {
        Q.push(v);
      }
    }
  }
}

long long solve(long long res, int a, int b) {

  long long ret = res;
  if (p[a] != p[b])
    ret -= 1LL * component[p[a]] * component[p[b]];
  merge(a, b);
  //  OUTV(component);
  return ret;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  //  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin >> N >> M;
  graph.resize(N);
  p.resize(N);
  component.resize(N);
  for (int i = 0; i < N; ++i) {
    p[i] = i;
    component[i] = 1;
  }
  ans.resize(M + 1);
  ans[M] = (1LL * N * (N - 1)) / 2;
  vector<int> A(M), B(M);
  for (int i = 0; i < M; ++i) {
    cin >> A[i];
    A[i]--;
    cin >> B[i];
    B[i]--;
  }

  for (int i = 0; i < M; ++i) {
    graph[A[M - 1 - i]].push_back(B[M - 1 - i]);
    graph[B[M - 1 - i]].push_back(A[M - 1 - i]);
    ans[M - 1 - i] = solve(ans[M - i], A[M - 1 - i], B[M - 1 - i]);
  }
  for (int i = 1; i <= M; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}