Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define For(i, n) for (int i = 0; i < n; i++)
#define Fre(i, a, b) for (int i = a; i < b; i++)
#define sf(n) scanf("%d", &n)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pfn(n) printf("%d\n", n)
#define pfs(n) printf("%d ", n)
#define ff first
#define ss second
#define mem(a, b) memset(a, b, sizeof(a))
#define READ freopen("in.txt", "r", stdin)
#define WRITE freopen("out.txt", "w", stdout)
#define sz size()
#define number_of_digit(n) __builtin_ffs(n);
#define ll long long
#define T int
#define ld long double
inline void print(T n) {
  if (n == 0) {
    putchar('0');
    putchar('\n');
  } else if (n == -1) {
    putchar('-');
    putchar('1');
    putchar('\n');
  } else {
    char buf[11];
    buf[10] = '\n';
    int i = 9;
    while (n) {
      buf[i--] = n % 10 + '0';
      n /= 10;
    }
    while (buf[i] != '\n')
      putchar(buf[++i]);
  }
}
int read() {
  int cc = getc(stdin);
  for (; cc < '0' || cc > '9';)
    cc = getc(stdin);
  int ret = 0;
  for (; cc >= '0' && cc <= '9';) {
    ret = ret * 10 + cc - '0';
    cc = getc(stdin);
  }
  return ret;
}
ll power(ll num, ll g) {
  if (g == 0)
    return 1;

  if (g % 2 == 1)
    return (num * power((num * num), g / 2));
  return power((num * num), g / 2);
}
ll a[100005];
ll find(ll index) {
  if (a[index] == index)
    return index;
  return a[index] = find(a[index]);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, m;
  cin >> n >> m;
  pair<ll, ll> pa[m];
  For(i, m) { cin >> pa[i].ff >> pa[i].ss; }

  For(i, n + 1) a[i] = i;
  ll size[n + 1];
  For(i, n + 1) size[i] = 1;
  ll ans = n;
  ans *= n - 1;
  ans /= 2;
  stack<ll> st;
  for (ll i = m - 1; i >= 0; i--) {
    st.push(ans);
    ll u = find(pa[i].ff);
    ll v = find(pa[i].ss);
    if (u == v) {
      // st.push(ans);
    } else {
      a[u] = a[v];
      ans -= size[v] * size[u];
      size[v] += size[u];
      // st.push(ans);
    }
  }
  while (!st.empty()) {
    cout << st.top() << endl;
    st.pop();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define For(i, n) for (int i = 0; i < n; i++)
#define Fre(i, a, b) for (int i = a; i < b; i++)
#define sf(n) scanf("%d", &n)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pfn(n) printf("%d\n", n)
#define pfs(n) printf("%d ", n)
#define ff first
#define ss second
#define mem(a, b) memset(a, b, sizeof(a))
#define READ freopen("in.txt", "r", stdin)
#define WRITE freopen("out.txt", "w", stdout)
#define sz size()
#define number_of_digit(n) __builtin_ffs(n);
#define ll long long
#define T int
#define ld long double
inline void print(T n) {
  if (n == 0) {
    putchar('0');
    putchar('\n');
  } else if (n == -1) {
    putchar('-');
    putchar('1');
    putchar('\n');
  } else {
    char buf[11];
    buf[10] = '\n';
    int i = 9;
    while (n) {
      buf[i--] = n % 10 + '0';
      n /= 10;
    }
    while (buf[i] != '\n')
      putchar(buf[++i]);
  }
}
int read() {
  int cc = getc(stdin);
  for (; cc < '0' || cc > '9';)
    cc = getc(stdin);
  int ret = 0;
  for (; cc >= '0' && cc <= '9';) {
    ret = ret * 10 + cc - '0';
    cc = getc(stdin);
  }
  return ret;
}
ll power(ll num, ll g) {
  if (g == 0)
    return 1;

  if (g % 2 == 1)
    return (num * power((num * num), g / 2));
  return power((num * num), g / 2);
}
ll a[100005];
ll find(ll index) {
  if (a[index] == index)
    return index;
  return a[index] = find(a[index]);
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, m;
  cin >> n >> m;
  pair<ll, ll> pa[m];
  For(i, m) { cin >> pa[i].ff >> pa[i].ss; }

  For(i, n + 1) a[i] = i;
  ll size[n + 1];
  For(i, n + 1) size[i] = 1;
  ll ans = n;
  ans *= n - 1;
  ans /= 2;
  stack<ll> st;
  for (ll i = m - 1; i >= 0; i--) {
    st.push(ans);
    ll u = find(pa[i].ff);
    ll v = find(pa[i].ss);
    if (u == v) {
      // st.push(ans);
    } else {
      a[u] = a[v];
      ans -= size[v] * size[u];
      size[v] += size[u];
      // st.push(ans);
    }
  }
  while (!st.empty()) {
    cout << st.top() << endl;
    st.pop();
  }
  return 0;
}