Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, from, to) for (long long int i = (from); i < (to); i++)
#define REV(v) for (auto itr = v.rbegin(); itr != v.rend(); itr++)
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define SORT(v) sort(ALL(v));
#define LAST(v) v[v.size() - 1]
#define ZERO(a) memset(a, 0, sizeof(a));
#define MINUS(a) memset(a, -1, sizeof(a));
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define pi pair<int, int>
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define IIN(a)                                                                 \
  int a;                                                                       \
  cin >> a;
#define LIN(a)                                                                 \
  ll a;                                                                        \
  cin >> a;
#define SIN(a)                                                                 \
  string a;                                                                    \
  cin >> a;
#define VL(v, n) vector<ll> v(n);
#define Read_VL(v, n)                                                          \
  vector<ll> v(n);                                                             \
  REP(i, 0, n) cin >> v[i];
#define sumV(v, n)                                                             \
  Vl(acm, n) REP(i, 0, n) acm[i] = v[i] + (i == 0 ? 0 : acm[i - 1]);
#define EPS (1e-10)
#define INF 1000000000000LL

void YN(bool b) { cout << (b ? "YES" : "NO") << "\n"; }
void Yn(bool b) { cout << (b ? "Yes" : "No") << "\n"; }
void yn(bool b) { cout << (b ? "yes" : "no") << "\n"; }

template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }

template <class T> void puta(T &&t) { cout << t << "\n"; }
template <class H, class... T> void puta(H &&h, T &&...t) {
  cout << h << ' ';
  puta(t...);
}

template <typename ELEMENT_TYPE> struct _setfillw {
  ELEMENT_TYPE c;
  std::streamsize w;
  _setfillw(ELEMENT_TYPE c, std::streamsize w) : w(w), c(c) {}
};

template <typename ELEMENT_TYPE>
inline _setfillw<ELEMENT_TYPE> setfillw(ELEMENT_TYPE c, std::streamsize w) {
  return _setfillw<ELEMENT_TYPE>(c, w);
}

template <typename ELEMENT_TYPE>
inline std::ostream &operator<<(std::ostream &os,
                                const _setfillw<ELEMENT_TYPE> &manip) {
  return os << std::setw(manip.w) << std::setfill(manip.c);
}

template <class T = ll> struct Graph {
  int n;
  vector<vector<tuple<ll, T>>> edge;
  Graph(int N = 1) : n(N) { edge.resize(n); }
  void add(ll f, ll t, T c, bool d = false) {
    edge[f].emplace_back(t, c);
    if (!d)
      edge[t].emplace_back(f, c);
  }
  void view() {
    REP(i, 0, n)
    for (auto &e : edge[i]) puta(i, "=>", get<0>(e), ", cost :", get<1>(e));
  }
};

bool length_cmp(string &a, string &b) { return a.size() < b.size(); }

ll StoLL(string s) {
  ll num = 0;
  REP(i, 0, s.size()) { num = num * 10 + (s[i] ^ 48); }
  return num;
}

ll CtoLL(char c) {
  if ('a' <= c && c <= 'z') {
    return (ll)(c ^ 96);
  } else if ('A' <= c && c <= 'Z') {
    return (ll)(c ^ 64);
  }
  return 0;
}

ll MOD = 1000000007;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

#include <array>
#include <numeric>

struct UF {
  vector<ll> par, sz;
  UF() {}
  UF(ll n) : par(n), sz(n, 1) { iota(par.begin(), par.end(), 0); }
  ll find(ll x) {
    if (x == par[x])
      return x;
    return par[x] = find(par[x]);
  }
  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] < sz[y])
      swap(x, y);
    sz[x] += sz[y];
    par[y] = x;
  }
  bool same(ll x, ll y) { return find(x) == find(y); }
};

signed main() {
  LIN(n) LIN(m) UF uf(n);
  vector<ll> ans(n), a(n), b(n);
  REP(i, 0, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  ans[m - 1] = n * (n - 1) / 2;
  for (ll i = m - 1; i >= 1; i--) {
    if (uf.same(a[i], b[i])) {
      ans[i - 1] = ans[i];
    } else {
      ll p = uf.sz[uf.find(a[i])];
      ll q = uf.sz[uf.find(b[i])];
      uf.unite(a[i], b[i]);
      ans[i - 1] = ans[i] - p * q;
    }
  }
  REP(i, 0, ans.size()) { puta(ans[i]); }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, from, to) for (long long int i = (from); i < (to); i++)
#define REV(v) for (auto itr = v.rbegin(); itr != v.rend(); itr++)
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define SORT(v) sort(ALL(v));
#define LAST(v) v[v.size() - 1]
#define ZERO(a) memset(a, 0, sizeof(a));
#define MINUS(a) memset(a, -1, sizeof(a));
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define pi pair<int, int>
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define IIN(a)                                                                 \
  int a;                                                                       \
  cin >> a;
#define LIN(a)                                                                 \
  ll a;                                                                        \
  cin >> a;
#define SIN(a)                                                                 \
  string a;                                                                    \
  cin >> a;
#define VL(v, n) vector<ll> v(n);
#define Read_VL(v, n)                                                          \
  vector<ll> v(n);                                                             \
  REP(i, 0, n) cin >> v[i];
#define sumV(v, n)                                                             \
  Vl(acm, n) REP(i, 0, n) acm[i] = v[i] + (i == 0 ? 0 : acm[i - 1]);
#define EPS (1e-10)
#define INF 1000000000000LL

void YN(bool b) { cout << (b ? "YES" : "NO") << "\n"; }
void Yn(bool b) { cout << (b ? "Yes" : "No") << "\n"; }
void yn(bool b) { cout << (b ? "yes" : "no") << "\n"; }

template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }

template <class T> void puta(T &&t) { cout << t << "\n"; }
template <class H, class... T> void puta(H &&h, T &&...t) {
  cout << h << ' ';
  puta(t...);
}

template <typename ELEMENT_TYPE> struct _setfillw {
  ELEMENT_TYPE c;
  std::streamsize w;
  _setfillw(ELEMENT_TYPE c, std::streamsize w) : w(w), c(c) {}
};

template <typename ELEMENT_TYPE>
inline _setfillw<ELEMENT_TYPE> setfillw(ELEMENT_TYPE c, std::streamsize w) {
  return _setfillw<ELEMENT_TYPE>(c, w);
}

template <typename ELEMENT_TYPE>
inline std::ostream &operator<<(std::ostream &os,
                                const _setfillw<ELEMENT_TYPE> &manip) {
  return os << std::setw(manip.w) << std::setfill(manip.c);
}

template <class T = ll> struct Graph {
  int n;
  vector<vector<tuple<ll, T>>> edge;
  Graph(int N = 1) : n(N) { edge.resize(n); }
  void add(ll f, ll t, T c, bool d = false) {
    edge[f].emplace_back(t, c);
    if (!d)
      edge[t].emplace_back(f, c);
  }
  void view() {
    REP(i, 0, n)
    for (auto &e : edge[i]) puta(i, "=>", get<0>(e), ", cost :", get<1>(e));
  }
};

bool length_cmp(string &a, string &b) { return a.size() < b.size(); }

ll StoLL(string s) {
  ll num = 0;
  REP(i, 0, s.size()) { num = num * 10 + (s[i] ^ 48); }
  return num;
}

ll CtoLL(char c) {
  if ('a' <= c && c <= 'z') {
    return (ll)(c ^ 96);
  } else if ('A' <= c && c <= 'Z') {
    return (ll)(c ^ 64);
  }
  return 0;
}

ll MOD = 1000000007;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

#include <array>
#include <numeric>

struct UF {
  vector<ll> par, sz;
  UF() {}
  UF(ll n) : par(n), sz(n, 1) { iota(par.begin(), par.end(), 0); }
  ll find(ll x) {
    if (x == par[x])
      return x;
    return par[x] = find(par[x]);
  }
  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] < sz[y])
      swap(x, y);
    sz[x] += sz[y];
    par[y] = x;
  }
  bool same(ll x, ll y) { return find(x) == find(y); }
};

signed main() {
  LIN(n) LIN(m) UF uf(n);
  vector<ll> ans(m), a(m), b(m);
  REP(i, 0, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  ans[m - 1] = n * (n - 1) / 2;
  for (ll i = m - 1; i >= 1; i--) {
    if (uf.same(a[i], b[i])) {
      ans[i - 1] = ans[i];
    } else {
      ll p = uf.sz[uf.find(a[i])];
      ll q = uf.sz[uf.find(b[i])];
      uf.unite(a[i], b[i]);
      ans[i - 1] = ans[i] - p * q;
    }
  }
  REP(i, 0, ans.size()) { puta(ans[i]); }
  return 0;
}