Problem p03108 (C++)

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

struct Unionfind {
  // tree number
  vector<int> par;
  // tree rank
  vector<int> treerank;
  // constructor
  Unionfind(int n = 1) { stree(n + 1); }
  // make and initialization
  void stree(int n = 1) {
    par.resize(n);
    treerank.resize(n);
    for (int i = 0; i < n; ++i) {
      par[i] = -1;
      treerank[i] = 0;
    }
  }
  // search root
  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  // is same?
  bool issame(int x, int y) { return root(x) == root(y); }

  // add
  // already added, return 0
  bool uni(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return 0;
    if (treerank[x] < treerank[y]) {
      par[root(y)] += size(x);
      par[x] = y;
    } else if (treerank[y] < treerank[x]) {
      par[root(x)] += size(y);
      par[y] = x;
    } else {
      par[root(x)] += size(y);
      par[y] = x;
      ++treerank[x];
    }
    return 1;
  }
  int size(int x) { return -par[root(x)]; }
};

long long n, m;
long long cnt[100005] = {0};
vector<pair<int, int>> v;
Unionfind uf;
vector<long long> ans;

void solve();

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    v.push_back(make_pair(a, b));
  }
  reverse(v.begin(), v.end());
  ans.assign(m, 0);
  solve();
  for (int i = 0; i < m; ++i)
    cout << ans[m - 1 - i] << endl;
  return 0;
}

void solve() {
  ans[0] = n * (n - 1) / 2;
  uf = Unionfind(n + 1);
  for (int i = 0; i < n; ++i)
    cnt[i] = 1;
  for (int i = 0; i < m - 1; ++i) {
    long long now = 0;
    if (uf.issame(v[i].fi, v[i].se)) {
      ans[i + 1] = ans[i];
      continue;
    }
    now = uf.size(v[i].fi);
    now *= uf.size(v[i].se);
    uf.uni(v[i].fi, v[i].se);
    ans[i + 1] = ans[i] - now;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;

struct Unionfind {
  // tree number
  vector<int> par;
  // tree rank
  vector<int> treerank;
  // constructor
  Unionfind(int n = 1) { stree(n + 1); }
  // make and initialization
  void stree(int n = 1) {
    par.resize(n);
    treerank.resize(n);
    for (int i = 0; i < n; ++i) {
      par[i] = -1;
      treerank[i] = 0;
    }
  }
  // search root
  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  // is same?
  bool issame(int x, int y) { return root(x) == root(y); }

  // add
  // already added, return 0
  bool uni(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return 0;
    if (treerank[x] > treerank[y])
      swap(x, y);
    if (treerank[x] == treerank[y])
      ++treerank[y];
    par[y] -= size(x);
    par[x] = y;
    return 1;
  }
  int size(int x) { return -par[root(x)]; }
};

long long n, m;
long long cnt[100005] = {0};
vector<pair<int, int>> v;
Unionfind uf;
vector<long long> ans;

void solve();

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    v.push_back(make_pair(a, b));
  }
  reverse(v.begin(), v.end());
  ans.assign(m, 0);
  solve();
  for (int i = 0; i < m; ++i)
    cout << ans[m - 1 - i] << endl;
  return 0;
}

void solve() {
  ans[0] = n * (n - 1) / 2;
  uf = Unionfind(n + 1);
  for (int i = 0; i < n; ++i)
    cnt[i] = 1;
  for (int i = 0; i < m - 1; ++i) {
    long long now = 0;
    if (uf.issame(v[i].fi, v[i].se)) {
      ans[i + 1] = ans[i];
      continue;
    }
    now = uf.size(v[i].fi);
    now *= uf.size(v[i].se);
    uf.uni(v[i].fi, v[i].se);
    ans[i + 1] = ans[i] - now;
  }
}