prithivMLmods/Coder-Stat huggingface dataset:
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define String std::string
#define fir first
#define sec second
#define mp std::make_pair
#define Pair std::pair<int, int>
#define Map std::map<int, int>
#define Vector std::vector<int>

typedef long long ll;
typedef unsigned long long ull;

const int N = 100000 + 5;
const int M = 3000 + 5;
const int MOD = 1e9 + 7;
const int inv2 = 500000004;
const int dx[] = {0, 1, -1, 0, 1, -1, 1, -1};
const int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};

template <class T> inline T readT() {
  T res = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    res = (res << 1) + (res << 3) + ch - '0';
  return res * f;
}
#define read readT<int>

int n, m;
Pair a[N];
int fa[N], size[N];
ll ans[N];

inline int find(int x) { return x == fa[x] ? fa[x] : find(fa[x]); }
inline ll merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx == fy)
    return 0;
  ll res = (ll)size[fx] * size[fy];
  fa[fx] = fy;
  size[fy] += size[fx];
  return res;
}

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
    size[i] = 1;
  }
  for (int i = 1; i <= m; i++) {
    a[i].fir = read();
    a[i].sec = read();
  }
  for (int i = m; i >= 1; i--) {
    // printf("%lld\n", merge(a[i].fir, a[i].sec));
    ans[i] = merge(a[i].fir, a[i].sec);
  }
  for (int i = 1; i <= m; i++) {
    ans[i] += ans[i - 1];
    printf("%lld\n", ans[i]);
  }
  return 0;
}

Fixed submission:
// #pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define String std::string
#define fir first
#define sec second
#define mp std::make_pair
#define Pair std::pair<int, int>
#define Map std::map<int, int>
#define Vector std::vector<int>

typedef long long ll;
typedef unsigned long long ull;

const int N = 100000 + 5;
const int M = 3000 + 5;
const int MOD = 1e9 + 7;
const int inv2 = 500000004;
const int dx[] = {0, 1, -1, 0, 1, -1, 1, -1};
const int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};

template <class T> inline T readT() {
  T res = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    res = (res << 1) + (res << 3) + ch - '0';
  return res * f;
}
#define read readT<int>

int n, m;
Pair a[N];
int fa[N], size[N];
ll ans[N];

inline int find(int x) { return x == fa[x] ? fa[x] : fa[x] = find(fa[x]); }
inline ll merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx == fy)
    return 0;
  ll res = (ll)size[fx] * size[fy];
  fa[fx] = fy;
  size[fy] += size[fx];
  return res;
}

int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
    size[i] = 1;
  }
  for (int i = 1; i <= m; i++) {
    a[i].fir = read();
    a[i].sec = read();
  }
  for (int i = m; i >= 1; i--) {
    // printf("%lld\n", merge(a[i].fir, a[i].sec));
    ans[i] = merge(a[i].fir, a[i].sec);
  }
  for (int i = 1; i <= m; i++) {
    ans[i] += ans[i - 1];
    printf("%lld\n", ans[i]);
  }
  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>

#define DEBUG(x) cout << #x << ": " << x << endl

using namespace std;

class UnionFind {
public:
  vector<int> Parent;

  UnionFind(int N) { Parent = vector<int>(N, -1); }

  int root(int A) {
    if (Parent[A] < 0)
      return A;
    else
      return Parent[A] = root(Parent[A]);
  }

  int size(int A) { return -Parent[root(A)]; }

  bool connect(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (size(A) < size(B))
      swap(A, B);
    Parent[A] += Parent[B];
    Parent[B] = Parent[A];
    return true;
  }
};

int main() {

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

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

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

  UnionFind Uni(N);

  for (int i = M - 1; i >= 1; i--) {
    ans[i - 1] = ans[i];
    if (Uni.root(A[i]) != Uni.root(B[i])) {
      ans[i - 1] -= (long long)Uni.size(A[i]) * Uni.size(B[i]);
      Uni.connect(A[i], B[i]);
    }
  }

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>

#define DEBUG(x) cout << #x << ": " << x << endl

using namespace std;

class UnionFind {
public:
  vector<int> Parent;

  UnionFind(int N) { Parent = vector<int>(N, -1); }

  int root(int A) {
    if (Parent[A] < 0)
      return A;
    else
      return Parent[A] = root(Parent[A]);
  }

  int size(int A) { return -Parent[root(A)]; }

  bool connect(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (size(A) < size(B))
      swap(A, B);
    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
};

int main() {

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

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

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

  UnionFind Uni(N);

  for (int i = M - 1; i >= 1; i--) {
    ans[i - 1] = ans[i];
    if (Uni.root(A[i]) != Uni.root(B[i])) {
      ans[i - 1] -= (long long)Uni.size(A[i]) * Uni.size(B[i]);
      Uni.connect(A[i], B[i]);
    }
  }

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

  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

class UnionFind {
  vector<int> par, sz;

public:
  UnionFind() {}
  UnionFind(int n) {
    par = sz = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y]) {
      par[y] = x;
      sz[x] += sz[y];
    } else {
      par[x] = y;
      sz[y] += sz[x];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};
int a[200000], b[200000];
int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) { scanf("%d%d", &a[i], &b[i]); }
  UnionFind uf(n);
  ll ans = n * (ll)(n - 1) / 2;
  vector<ll> v;
  for (int i = m - 1; i >= 0; i--) {
    v.push_back(ans);
    if (uf.same(a[i], b[i]))
      continue;
    ans -= uf.size(a[i]) * (ll)uf.size(b[i]);
    uf.unite(a[i], b[i]);
  }
  reverse(v.begin(), v.end());
  for (ll i : v) {
    printf("%lld\n", i);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

class UnionFind {
  vector<int> par, sz;

public:
  UnionFind() {}
  UnionFind(int n) {
    par = sz = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y]) {
      par[y] = x;
      sz[x] += sz[y];
    } else {
      par[x] = y;
      sz[y] += sz[x];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};
int a[200000], b[200000];
int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    scanf("%d%d", &a[i], &b[i]);
    a[i]--;
    b[i]--;
  }
  UnionFind uf(n);
  ll ans = n * (ll)(n - 1) / 2;
  vector<ll> v;
  for (int i = m - 1; i >= 0; i--) {
    v.push_back(ans);
    if (uf.same(a[i], b[i]))
      continue;
    ans -= uf.size(a[i]) * (ll)uf.size(b[i]);
    uf.unite(a[i], b[i]);
  }
  reverse(v.begin(), v.end());
  for (ll i : v) {
    printf("%lld\n", i);
  }
}
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll par[100005], sum[100005], bridg[100005][2], ans[100005];
ll find(ll x) { return par[x] == x ? x : find(par[x]); }
int main() {
  ll n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    par[i] = i;
    sum[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    scanf("%lld%lld", &bridg[i][0], &bridg[i][1]);
  }
  ans[m - 1] = n * (n - 1) / 2;
  for (int i = m - 1; i > 0; i--) {
    ll x, y;
    x = find(bridg[i][0]);
    y = find(bridg[i][1]);
    if (x != y) {
      ans[i - 1] = ans[i] - sum[x] * sum[y];
      sum[y] += sum[x];
      par[x] = y;
    } else
      ans[i - 1] = ans[i];
  }
  for (int i = 0; i < m; i++)
    printf("%lld\n", ans[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll par[100005], sum[100005], bridg[100005][2], ans[100005];
ll find(ll x) { return par[x] == x ? x : par[x] = find(par[x]); }
int main() {
  ll n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    par[i] = i;
    sum[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    scanf("%lld%lld", &bridg[i][0], &bridg[i][1]);
  }
  ans[m - 1] = n * (n - 1) / 2;
  for (int i = m - 1; i > 0; i--) {
    ll x, y;
    x = find(bridg[i][0]);
    y = find(bridg[i][1]);
    if (x != y) {
      ans[i - 1] = ans[i] - sum[x] * sum[y];
      sum[y] += sum[x];
      par[x] = y;
    } else
      ans[i - 1] = ans[i];
  }
  for (int i = 0; i < m; i++)
    printf("%lld\n", ans[i]);
  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
ll p[100002];
ll find(ll x) { return p[x] == x ? x : find(p[x]); }
int main() {
  IOS;
  ll n, m, i;
  cin >> n >> m;
  ll link[m][2], sum[n], ans[m];
  for (i = 0; i < n; i++) {
    p[i] = i;
    sum[i] = 1;
  }
  for (i = 0; i < m; i++) {
    cin >> link[i][0] >> link[i][1];
  }
  ans[m - 1] = (n * (n - 1) / 2);
  for (i = m - 1; i >= 0; i--) {
    ll q, w;
    q = find(link[i][0]);
    w = find(link[i][1]);
    if (q != w) {
      ans[i - 1] = ans[i] - (sum[q] * sum[w]);
      sum[w] += sum[q];
      p[q] = w;
    } else
      ans[i - 1] = ans[i];
  }
  for (auto g : ans)
    cout << g << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
ll p[100002];
ll find(ll x) {
  return p[x] == x ? x
                   : p[x] = find(p[x]); // p[x]=find(p[x]) necessary else TLE as
                                        // it takes longer time to find parent
}
int main() {
  IOS;
  ll n, m, i;
  cin >> n >> m;
  ll link[m][2], sum[n], ans[m];
  for (i = 0; i < n; i++) {
    p[i] = i;
    sum[i] = 1;
  }
  for (i = 0; i < m; i++) {
    cin >> link[i][0] >> link[i][1];
  }
  ans[m - 1] = (n * (n - 1) / 2);
  for (i = m - 1; i >= 0; i--) {
    ll q, w;
    q = find(link[i][0]);
    w = find(link[i][1]);
    if (q != w) {
      ans[i - 1] = ans[i] - (sum[q] * sum[w]);
      sum[w] += sum[q];
      p[q] = w;
    } else
      ans[i - 1] = ans[i];
  }
  for (auto g : ans)
    cout << g << endl;
  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define ll long long
using namespace std;

const int _M = 1e5 + 40;

ll a[_M], t[_M], ans[_M], k = 0;

struct {
  int x, y;
} e[_M];

int fd(int x) { return x == a[x] ? x : fd(a[x]); }

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    a[i] = i, t[i] = 1;
  for (int i = 1; i <= m; i++)
    scanf("%d%d", &e[i].x, &e[i].y);
  ll an = (ll)n * (ll)(n - 1) / 2;
  ans[k++] = an;
  // cout<<an<<endl;
  for (int i = m; i >= 1; i--) {
    int ra = fd(e[i].x), rb = fd(e[i].y);
    if (ra != rb) {
      an -= ((ll)t[ra] * (ll)t[rb]);
      t[ra] += t[rb];
      a[rb] = ra;
    }
    ans[k++] = an;
  }
  k -= 2;
  while (k >= 0)
    cout << ans[k--] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define ll long long
using namespace std;

const int _M = 1e5 + 40;

ll a[_M], t[_M], ans[_M], k = 0;

struct {
  int x, y;
} e[_M];

int fd(int x) { return x == a[x] ? x : a[x] = fd(a[x]); }

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    a[i] = i, t[i] = 1;
  for (int i = 1; i <= m; i++)
    scanf("%d%d", &e[i].x, &e[i].y);
  ll an = (ll)n * (ll)(n - 1) / 2;
  ans[k++] = an;
  // cout<<an<<endl;
  for (int i = m; i >= 1; i--) {
    int ra = fd(e[i].x), rb = fd(e[i].y);
    if (ra != rb) {
      an -= ((ll)t[ra] * (ll)t[rb]);
      t[ra] += t[rb];
      a[rb] = ra;
    }
    ans[k++] = an;
  }
  k -= 2;
  while (k >= 0)
    cout << ans[k--] << endl;
  return 0;
}
Problem p03108 (C++)

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

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

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

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  pair<int, int> ab[m];
  for (int i = 0; i < m; ++i) {
    cin >> ab[i].first >> ab[i].second;
  }
  reverse(ab, ab + m);
  long long now = (long long)n * (n - 1) / 2LL, ans[m];
  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    ans[i] = now;
    if (uf.issame(ab[i].first, ab[i].second)) {
      continue;
    }
    now -= (long long)uf.size(ab[i].first) * uf.size(ab[i].second);
    uf.merge(ab[i].first, ab[i].second);
  }
  reverse(ans, ans + m);
  for (auto v : ans) {
    cout << v << endl;
  }
  return 0;
}


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

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

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

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  pair<int, int> ab[m];
  for (int i = 0; i < m; ++i) {
    cin >> ab[i].first >> ab[i].second;
    --ab[i].first;
    --ab[i].second;
  }
  reverse(ab, ab + m);
  long long now = (long long)n * (n - 1) / 2LL, ans[m];
  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    ans[i] = now;
    if (uf.issame(ab[i].first, ab[i].second)) {
      continue;
    }
    now -= (long long)uf.size(ab[i].first) * uf.size(ab[i].second);
    uf.merge(ab[i].first, ab[i].second);
  }
  reverse(ans, ans + m);
  for (auto v : ans) {
    cout << v << endl;
  }
  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
typedef long long ll;
using namespace std;
ll parent[10001];
ll s[10001];
ll input[10001][2];
ll ans[10001];
ll sum;
ll Find(ll x) {
  if (x == parent[x]) {
    return x;
  } else {
    ll p = Find(parent[x]);
    parent[x] = p;
    return p;
  }
}
void Union(ll x, ll y) {
  x = Find(x);
  y = Find(y);
  if (x != y) {
    parent[y] = x;
    sum += s[x] * (s[x] - 1) / 2;
    sum += s[y] * (s[y] - 1) / 2;
    s[x] += s[y];
    sum -= s[x] * (s[x] - 1) / 2;
  }
}
int main() {
  ll n, m;
  scanf("%lld %lld", &n, &m);
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    s[i] = 1;
  }
  for (int i = 0; i < m; i++)
    scanf("%lld %lld", &input[i][0], &input[i][1]);
  sum = n * (n - 1) / 2;
  for (int i = m - 1; i >= 0; i--) {
    ans[i] = sum;
    Union(input[i][0], input[i][1]);
  }
  for (int i = 0; i < m; i++)
    printf("%lld\n", ans[i]);
  return 0;
}

Fixed submission:
#include <iostream>
typedef long long ll;
using namespace std;
ll parent[100001];
ll s[100001];
ll input[100001][2];
ll ans[100001];
ll sum;
ll Find(ll x) {
  if (x == parent[x]) {
    return x;
  } else {
    ll p = Find(parent[x]);
    parent[x] = p;
    return p;
  }
}
void Union(ll x, ll y) {
  x = Find(x);
  y = Find(y);
  if (x != y) {
    parent[y] = x;
    sum += s[x] * (s[x] - 1) / 2;
    sum += s[y] * (s[y] - 1) / 2;
    s[x] += s[y];
    sum -= s[x] * (s[x] - 1) / 2;
  }
}
int main() {
  ll n, m;
  scanf("%lld %lld", &n, &m);
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    s[i] = 1;
  }
  for (int i = 0; i < m; i++)
    scanf("%lld %lld", &input[i][0], &input[i][1]);
  sum = n * (n - 1) / 2;
  for (int i = m - 1; i >= 0; i--) {
    ans[i] = sum;
    Union(input[i][0], input[i][1]);
  }
  for (int i = 0; i < m; i++)
    printf("%lld\n", ans[i]);
  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define sz(v) (int(v.size()))
typedef unsigned long long ul;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vii;
typedef pair<int, int> pii;
typedef vector<pii> vp;
int OO = 2e9;
const double eps = 1e-9;
const int N = 200;
int mx = -1e9, mn = 1e9;
int arr[N], arr2[N];

struct dsu {
  int *par, *sz;
  dsu(int n) {
    par = new int[n + 1];
    sz = new int[n + 1];
    lp(i, n + 1) {
      par[i] = i;
      sz[i] = 1;
    }
  }

  int get_root(int a) {
    if (par[a] == a)
      return a;
    return get_root(par[a]);
  }

  long long Union(int a, int b) {
    int apar = get_root(a), bpar = get_root(b);
    if (apar == bpar)
      return 0;
    par[apar] = bpar;
    int tmp = sz[bpar];
    sz[bpar] += sz[apar];
    return 1LL * tmp * sz[apar];
  }
};

int main() {

  int n, m;
  cin >> n >> m;
  vector<pii> edges(m);
  for (int i = 0; i < m; ++i) {
    cin >> edges[i].first >> edges[i].second;
  }

  dsu ds(n);
  vector<long long> ans(m);
  long long cnt = 1LL * n * (n - 1) / 2;
  for (int i = m - 1; i >= 0; --i) {
    ans[i] = cnt;
    cnt -= ds.Union(edges[i].first, edges[i].second);
  }

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define sz(v) (int(v.size()))
typedef unsigned long long ul;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vii;
typedef pair<int, int> pii;
typedef vector<pii> vp;
int OO = 2e9;
const double eps = 1e-9;
const int N = 200;
int mx = -1e9, mn = 1e9;
int arr[N], arr2[N];

struct dsu {
  int *par, *sz;
  dsu(int n) {
    par = new int[n + 1];
    sz = new int[n + 1];
    lp(i, n + 1) {
      par[i] = i;
      sz[i] = 1;
    }
  }

  int get_root(int a) {
    if (par[a] == a)
      return a;
    return get_root(par[a]);
  }

  long long Union(int a, int b) {
    int apar = get_root(a), bpar = get_root(b);
    if (apar == bpar)
      return 0;
    int tmp1 = sz[bpar];
    int tmp2 = sz[apar];
    if (sz[bpar] > sz[apar]) {
      par[apar] = bpar;
      sz[bpar] += sz[apar];
    } else {
      par[bpar] = apar;
      sz[apar] += sz[bpar];
    }
    return 1LL * tmp1 * tmp2;
  }
};

int main() {

  int n, m;
  cin >> n >> m;
  vector<pii> edges(m);
  for (int i = 0; i < m; ++i) {
    cin >> edges[i].first >> edges[i].second;
  }

  dsu ds(n);
  vector<long long> ans(m);
  long long cnt = 1LL * n * (n - 1) / 2;
  for (int i = m - 1; i >= 0; --i) {
    ans[i] = cnt;
    cnt -= ds.Union(edges[i].first, edges[i].second);
  }

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

  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

class QuickUnion {
public:
  vector<int> id;
  vector<long long int> size;
  void init(long long int s) {
    id.resize(s);
    iota(id.begin(), id.end(), 0);
    vector<long long int> vec(s, 1LL);
    size = vec;
  }
  int root(int i) {
    while (i != id[i])
      i = id[i];
    return i;
  }
  long long int unite(int p, int q) {
    int i = root(p);
    int j = root(q);
    if (i == j)
      return 0LL;
    else {
      long long int res = size[i] * size[j];
      size[j] = size[i] + size[j];
      id[i] = j;
      return res;
    }
  }
};

int main() {
  long long int n;
  int m;
  cin >> n >> m;
  vector<pair<int, int>> v(m);
  for (int i = 0; i < m; i++) {
    cin >> v[m - i - 1].first >> v[m - i - 1].second;
    v[m - i - 1].first--;
    v[m - i - 1].second--;
  }

  QuickUnion islands;
  islands.init(n);

  vector<long long int> ans(m);
  ans[0] = n * (n - 1) / 2;
  for (int i = 0; i < m - 1; i++) {
    if (ans[i] == 0)
      break;
    else
      ans[i + 1] = ans[i] - islands.unite(v[i].first, v[i].second);
  }

  for (int i = 0; i < m; i++)
    cout << ans[m - 1 - i] << endl;
}


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

class QuickUnion {
public:
  vector<int> id;
  vector<long long int> size;
  void init(long long int s) {
    id.resize(s);
    iota(id.begin(), id.end(), 0);
    vector<long long int> vec(s, 1LL);
    size = vec;
  }
  int root(int i) {
    while (i != id[i])
      i = id[i];
    return i;
  }
  long long int unite(int p, int q) {
    int i = root(p);
    int j = root(q);
    if (i == j)
      return 0LL;
    else {
      long long int res = size[i] * size[j];
      if (size[i] < size[j]) {
        id[i] = j;
        size[j] = size[i] + size[j];
      } else {
        id[j] = i;
        size[i] = size[i] + size[j];
      }
      return res;
    }
  }
};

int main() {
  long long int n;
  int m;
  cin >> n >> m;
  vector<pair<int, int>> v(m);
  for (int i = 0; i < m; i++) {
    cin >> v[m - i - 1].first >> v[m - i - 1].second;
    v[m - i - 1].first--;
    v[m - i - 1].second--;
  }

  QuickUnion islands;
  islands.init(n);

  vector<long long int> ans(m);
  ans[0] = n * (n - 1) / 2;
  for (int i = 0; i < m - 1; i++) {
    if (ans[i] == 0)
      break;
    else
      ans[i + 1] = ans[i] - islands.unite(v[i].first, v[i].second);
  }

  for (int i = 0; i < m; i++)
    cout << ans[m - 1 - i] << endl;
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define rep(i, N) for (int i = 0; i < N; i++)

int node[100010] = {};

class UnionFind {
public:
  vector<int> node;

  UnionFind(int N) { node = vector<int>(N, -1); }

  int unite(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b) {
      return false;
    }
    if (size(a) < size(b))
      swap(a, b);

    node[a] += node[b];
    node[b] = a;
    return true;
  }

  int find(int a) {
    if (node[a] < 0)
      return a;
    else
      return node[a] = find(node[a]);
  }

  int size(int a) { return -node[find(a)]; }
};

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

  rep(i, M) cin >> A[i] >> B[i];

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

  UnionFind uf(N);

  for (int i = M - 1; i >= 1; i--) {
    if (uf.find(A[i]) != uf.find(B[i])) {
      ans[i - 1] = ans[i] - (ll)uf.size(A[i]) * uf.size(B[i]);
      uf.unite(A[i], B[i]);
    } else
      ans[i - 1] = ans[i];
  }

  rep(i, M) cout << ans[i] << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define rep(i, N) for (int i = 0; i < N; i++)

int node[100010] = {};

class UnionFind {
public:
  vector<int> node;

  UnionFind(int N) { node = vector<int>(N, -1); }

  int unite(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b) {
      return false;
    }
    if (size(a) < size(b))
      swap(a, b);

    node[a] += node[b];
    node[b] = a;
    return true;
  }

  int find(int a) {
    if (node[a] < 0)
      return a;
    else
      return node[a] = find(node[a]);
  }

  int size(int a) { return -node[find(a)]; }
};

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

  rep(i, M) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

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

  UnionFind uf(N);

  for (int i = M - 1; i >= 1; i--) {
    if (uf.find(A[i]) != uf.find(B[i])) {
      ans[i - 1] = ans[i] - (ll)uf.size(A[i]) * uf.size(B[i]);
      uf.unite(A[i], B[i]);
    } else
      ans[i - 1] = ans[i];
  }

  rep(i, M) cout << ans[i] << endl;
}
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
typedef long long LL;
using namespace std;

LL ans;
vector<LL> p;
vector<LL> cnt;

LL find_root(LL x) {
  if (p[x] != x)
    x = find_root(p[x]);
  return p[x];
}

void unite(LL x, LL y) {
  LL nx = find_root(x);
  LL ny = find_root(y);
  ans = 0;
  if (nx == ny)
    return;
  else {
    p[ny] = nx;
    ans = cnt[nx] * cnt[ny];
    cnt[nx] += cnt[ny];
  }
}

int main() {
  LL n, m;
  cin >> n >> m;
  p = vector<LL>(n);
  cnt = vector<LL>(n, 1);
  for (LL i = 0; i < n; i++)
    p[i] = i;

  vector<LL> a(m), b(m);
  for (LL i = 0; i < m; i++) {
    LL s, t;
    scanf("%lld%lld", &s, &t);
    s--, t--;
    a[i] = s, b[i] = t;
  }

  vector<LL> res(m);
  res[m - 1] = n * (n - 1) / 2;
  for (LL i = m - 1; i >= 1; i--) {
    unite(a[i], b[i]);
    res[i - 1] = res[i] - ans;
  }

  for (LL i = 0; i < m; i++) {
    cout << res[i] << endl;
  }
}


Fixed submission:
#include <iostream>
#include <vector>
typedef long long LL;
using namespace std;

LL ans;
vector<LL> p;
vector<LL> cnt;

LL find_root(LL x) {
  if (p[x] != x)
    p[x] = find_root(p[x]);
  return p[x];
}

void unite(LL x, LL y) {
  LL nx = find_root(x);
  LL ny = find_root(y);
  ans = 0;
  if (nx == ny)
    return;
  else {
    p[ny] = nx;
    ans = cnt[nx] * cnt[ny];
    cnt[nx] += cnt[ny];
  }
}

int main() {
  LL n, m;
  cin >> n >> m;
  p = vector<LL>(n);
  cnt = vector<LL>(n, 1);
  for (LL i = 0; i < n; i++)
    p[i] = i;

  vector<LL> a(m), b(m);
  for (LL i = 0; i < m; i++) {
    LL s, t;
    scanf("%lld%lld", &s, &t);
    s--, t--;
    a[i] = s, b[i] = t;
  }

  vector<LL> res(m);
  res[m - 1] = n * (n - 1) / 2;
  for (LL i = m - 1; i >= 1; i--) {
    unite(a[i], b[i]);
    res[i - 1] = res[i] - ans;
  }

  for (LL i = 0; i < m; i++) {
    cout << res[i] << endl;
  }
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, i##_len = int(n); i < i##_len; ++i)
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define All(x) (x).begin(), (x).end()
#define rAll(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;

struct UnionFind {
  std::vector<int> par;
  ll huben;
  UnionFind(ll N) {
    par.resize(N, -1);
    huben = N * (N - 1) / 2;
  }
  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }
  int size(int x) { return -par[root(x)]; }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (same(x, y))
      return false;
    if (size(x) < size(y))
      swap(x, y);
    huben -= par[x] * par[y];
    par[x] += par[y];
    par[y] = x;
    return true;
  }
};
int main() {
  ll N, M;
  cin >> N >> M;
  vector<int> a(N), b(N);
  REP(i, M) cin >> a[i] >> b[i];
  vector<ll> ans(M);
  UnionFind uni(N);
  for (int i = M - 1; i >= 0; --i) {
    ans[i] = uni.huben;
    uni.unite(a[i] - 1, b[i] - 1);
  }
  REP(i, M) cout << ans[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, i##_len = int(n); i < i##_len; ++i)
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define All(x) (x).begin(), (x).end()
#define rAll(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;

struct UnionFind {
  std::vector<int> par;
  ll huben;
  UnionFind(ll N) {
    par.resize(N, -1);
    huben = N * (N - 1) / 2;
  }
  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }
  int size(int x) { return -par[root(x)]; }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (same(x, y))
      return false;
    if (size(x) < size(y))
      swap(x, y);
    huben -= par[x] * par[y];
    par[x] += par[y];
    par[y] = x;
    return true;
  }
};
int main() {
  ll N, M;
  cin >> N >> M;
  vector<int> a(M), b(M);
  REP(i, M) cin >> a[i] >> b[i];
  vector<ll> ans(M);
  UnionFind uni(N);
  for (int i = M - 1; i >= 0; --i) {
    ans[i] = uni.huben;
    uni.unite(a[i] - 1, b[i] - 1);
  }
  REP(i, M) cout << ans[i] << endl;
}
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 9;
long long result[maxn];

class dsu {
public:
  vector<int> parent, size;
  dsu(int n) : parent(n + 1), size(n + 1, 1) {
    for (int i = 0; i < n + 1; i++) {
      parent[i] = i;
    }
  }
  int find(int u) {
    if (u == parent[u])
      return u;
    return u = find(parent[u]);
  }
  bool same(int u, int v) {
    u = find(u);
    v = find(v);
    return (u == v);
  }
  void merge(int u, int v) {
    u = find(u);
    v = find(v);
    if (same(u, v))
      return;
    parent[u] = v;
    size[v] += size[u];
    size[u] = 0;
  }
  int get_size(int u) {
    u = find(u);
    return size[u];
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> edges;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    edges.push_back(make_pair(a, b));
  }
  long long inf = n * (n - 1LL) * 1LL / 2LL, tmp = inf;
  dsu uf(n);
  for (int i = 0; i < m; i++) {
    result[m - i - 1] = tmp;
    pair<int, int> edge = edges[m - i - 1];
    if (uf.same(edge.first, edge.second)) {
      continue;
    }
    int a = uf.get_size(edge.first), b = uf.get_size(edge.second);
    tmp += a * (a - 1LL) * 1LL / 2LL;
    tmp += b * (b - 1LL) * 1LL / 2LL;
    uf.merge(edge.first, edge.second);
    int size = uf.get_size(edge.first);
    tmp -= size * (size - 1LL) * 1LL / 2LL;
  }
  for (int i = 0; i < m; i++) {
    cout << result[i] << '\n';
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 9;
long long result[maxn];

class dsu {
public:
  vector<int> parent, size;
  dsu(int n) : parent(n + 1), size(n + 1, 1) {
    for (int i = 0; i < n + 1; i++) {
      parent[i] = i;
    }
  }
  int find(int u) {
    if (u == parent[u])
      return u;
    return parent[u] = find(parent[u]);
  }
  bool same(int u, int v) {
    u = find(u);
    v = find(v);
    return (u == v);
  }
  void merge(int u, int v) {
    u = find(u);
    v = find(v);
    if (same(u, v))
      return;
    parent[u] = v;
    size[v] += size[u];
    size[u] = 0;
  }
  int get_size(int u) {
    u = find(u);
    return size[u];
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> edges;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    edges.push_back(make_pair(a, b));
  }
  long long inf = n * (n - 1LL) * 1LL / 2LL, tmp = inf;
  dsu uf(n);
  for (int i = 0; i < m; i++) {
    result[m - i - 1] = tmp;
    pair<int, int> edge = edges[m - i - 1];
    if (uf.same(edge.first, edge.second)) {
      continue;
    }
    int a = uf.get_size(edge.first), b = uf.get_size(edge.second);
    tmp += a * (a - 1LL) * 1LL / 2LL;
    tmp += b * (b - 1LL) * 1LL / 2LL;
    uf.merge(edge.first, edge.second);
    int size = uf.get_size(edge.first);
    tmp -= size * (size - 1LL) * 1LL / 2LL;
  }
  for (int i = 0; i < m; i++) {
    cout << result[i] << '\n';
  }

  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

class UnionFind {
public:
  vector<int> Parent;

  UnionFind(int N) { Parent = vector<int>(N, -1); }

  int root(int A) {
    if (Parent[A] < 0)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  int size(int A) { return -Parent[root(A)]; }

  bool connect(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (size(A) < size(B))
      swap(A, B);
    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
};

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

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

  UnionFind Uni(N);

  for (int i = M - 1; i >= 0; i--) {
    ans[i - 1] = ans[i];
    if (Uni.root(A[i]) != Uni.root(B[i])) {
      ans[i - 1] -= (long long)Uni.size(A[i]) * Uni.size(B[i]);
      Uni.connect(A[i], B[i]);
    }
  }
  for (int i = 0; i < M; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

class UnionFind {
public:
  vector<int> Parent;

  UnionFind(int N) { Parent = vector<int>(N, -1); }

  int root(int A) {
    if (Parent[A] < 0)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  int size(int A) { return -Parent[root(A)]; }

  bool connect(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;

    if (size(A) < size(B))
      swap(A, B);
    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
};

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

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

  UnionFind Uni(N);

  for (int i = M - 1; i >= 1; i--) {
    ans[i - 1] = ans[i];
    if (Uni.root(A[i]) != Uni.root(B[i])) {
      ans[i - 1] -= (long long)Uni.size(A[i]) * Uni.size(B[i]);
      Uni.connect(A[i], B[i]);
    }
  }
  for (int i = 0; i < M; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, m, n) for (long i = m; i < n; i++)
#define repr(i, m, n) for (long i = n - 1; i >= m; i--)

struct UnionFind {
  vector<int> par, si;

  UnionFind(int N) : par(N), si(N, 1) { rep(i, 0, N) par[i] = i; }

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

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    si[y] += si[x];
    par[x] = y;
  }

  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return si[root(x)]; }
};

int main() {
  long n, m;
  cin >> n >> m;
  vector<long> a(n), b(n);
  UnionFind u(n);
  rep(i, 0, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  long num = n * (n - 1) / 2;
  vector<long> res;
  repr(i, 0, m) {
    res.push_back(num);

    if (!u.same(a[i], b[i])) {
      num -= u.size(a[i]) * u.size(b[i]);
      u.unite(a[i], b[i]);
    }
  }
  repr(i, 0, res.size()) cout << res[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, m, n) for (long i = m; i < n; i++)
#define repr(i, m, n) for (long i = n - 1; i >= m; i--)

struct UnionFind {
  vector<int> par, si;

  UnionFind(int N) : par(N), si(N, 1) { rep(i, 0, N) par[i] = i; }

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

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    si[y] += si[x];
    par[x] = y;
  }

  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return si[root(x)]; }
};

int main() {
  long n, m;
  cin >> n >> m;
  vector<long> a(m), b(m);
  UnionFind u(n);
  rep(i, 0, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  long num = n * (n - 1) / 2;
  vector<long> res;
  repr(i, 0, m) {
    res.push_back(num);

    if (!u.same(a[i], b[i])) {
      num -= u.size(a[i]) * u.size(b[i]);
      u.unite(a[i], b[i]);
    }
  }
  repr(i, 0, res.size()) cout << res[i] << endl;
}
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

const int NIL = -1;
long long n, m;
long long p[100009], num[100009];

void init() {
  for (int i = 0; i < n; i++) {
    p[i] = NIL;
    num[i] = 1;
  }
}

int parent(int a) {
  if (p[a] == NIL)
    return a;
  return parent(p[a]);
}

bool check(int a, int b) {
  if (parent(a) == parent(b))
    return true;
  return false;
}

int add(int a, int b) {
  if (check(a, b))
    return 0;
  long long pa = parent(a), pb = parent(b);
  long long ret = num[pa] * num[pb];
  num[pa] += num[pb];
  p[pb] = pa;
  return ret;
}

int main() {
  cin >> n >> m;
  int a[100009], b[100009];
  vector<long long> ans;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  init();
  ans.push_back(n * (n - 1) / 2);
  for (int i = m - 1; i >= 0; i--) {
    ans.push_back(ans[m - 1 - i] - add(a[i], b[i]));
  }
  for (int i = 0; i < m; i++) {
    ans[i] -= ans[m];
  }
  for (int i = m - 1; i >= 0; i--) {
    cout << ans[i] << endl;
  }
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

const int NIL = -1;
long long n, m;
long long p[100009], num[100009];

void init() {
  for (int i = 0; i < n; i++) {
    p[i] = NIL;
    num[i] = 1;
  }
}

int parent(int a) {
  if (p[a] == NIL)
    return a;
  return parent(p[a]);
}

bool check(int a, int b) {
  if (parent(a) == parent(b))
    return true;
  return false;
}

int add(int a, int b) {
  if (check(a, b))
    return 0;
  long long pa = parent(a), pb = parent(b);
  long long ret = num[pa] * num[pb];
  if (num[pa] < num[pb])
    swap(pa, pb);
  num[pa] += num[pb];
  p[pb] = pa;
  return ret;
}

int main() {
  cin >> n >> m;
  int a[100009], b[100009];
  vector<long long> ans;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  init();
  ans.push_back(n * (n - 1) / 2);
  for (int i = m - 1; i >= 0; i--) {
    ans.push_back(ans[m - 1 - i] - add(a[i], b[i]));
  }
  for (int i = 0; i < m; i++) {
    ans[i] -= ans[m];
  }
  for (int i = m - 1; i >= 0; i--) {
    cout << ans[i] << endl;
  }
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int MAX_N = 10000;
vector<pair<int, int>> edge;
int N, M;
int par[MAX_N];  // 親
ll sizes[MAX_N]; // 木の深さ
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    sizes[i] = 1;
  }
}
// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
// 併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (par[x] > par[y])
    swap(x, y); // merge technique
  par[y] = x;
  sizes[x] += sizes[y];
}

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

void solve() {
  init(N);
  ll ans[MAX_N];
  ans[M - 1] = (ll)N * (N - 1) / 2;
  for (int i = M - 1; i > 0; i--) {
    int a = edge[i].first;
    int b = edge[i].second;
    if (!same(a, b)) {
      ans[i - 1] = ans[i] - (ll)(sizes[find(a)] * sizes[find(b)]);
      unite(a, b);
    } else
      ans[i - 1] = ans[i];
  }
  for (int i = 0; i < M; i++)
    cout << ans[i] << '\n';
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a = a - 1;
    b = b - 1;
    edge.push_back(make_pair(a, b));
  }
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int MAX_N = 100000;
vector<pair<int, int>> edge;
int N, M;
int par[MAX_N];  // 親
ll sizes[MAX_N]; // 木の深さ
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    sizes[i] = 1;
  }
}
// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
// 併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (par[x] > par[y])
    swap(x, y); // merge technique
  par[y] = x;
  sizes[x] += sizes[y];
}

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

void solve() {
  init(N);
  ll ans[MAX_N];
  ans[M - 1] = (ll)N * (N - 1) / 2;
  for (int i = M - 1; i > 0; i--) {
    int a = edge[i].first;
    int b = edge[i].second;
    if (!same(a, b)) {
      ans[i - 1] = ans[i] - (ll)(sizes[find(a)] * sizes[find(b)]);
      unite(a, b);
    } else
      ans[i - 1] = ans[i];
  }
  for (int i = 0; i < M; i++)
    cout << ans[i] << '\n';
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a = a - 1;
    b = b - 1;
    edge.push_back(make_pair(a, b));
  }
  solve();
  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int maxn = 1500 + 5;
ll n1 = 0, n2 = 0;
ll root(ll arr[], ll a) {
  while (arr[a] != a) {
    arr[a] = arr[arr[a]];
    a = arr[a];
  }
  return a;
}
void unioun(ll arr[], ll size[], ll a, ll b) {
  int roota, rootb;
  roota = root(arr, a);
  rootb = root(arr, b);
  if (size[roota] < size[rootb]) {
    arr[roota] = arr[rootb];
    n1 = size[roota];
    n2 = size[rootb];
    size[rootb] += size[roota];
    size[roota] = 0;

  } else {
    arr[rootb] = arr[roota];
    n1 = size[roota];
    n2 = size[rootb];
    size[roota] += size[rootb];
  }
}
bool find(ll arr[], ll a, ll b) {
  int roota, rootb;
  roota = root(arr, a);
  rootb = root(arr, b);
  if (roota == rootb) {
    return true;
  }
  return false;
}
int main() {
  ll size[maxn], a[maxn], count[maxn], ans[maxn], i, x, y, n, m;
  cin >> n >> m;
  vector<pair<int, int>> v;
  for (i = 1; i < n + 1; i++) {
    a[i] = i;
    size[i] = 1;
    count[i] = 1;
  }
  //	v.push_back(make_pair(0,0));
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }
  // cout<<v[m-1].first<<' '<<v[m-1].second<<endl;
  ans[m - 1] = n * (n - 1) / 2;
  for (i = m - 2; i >= 0; i--) {
    if (!find(a, v[i + 1].first, v[i + 1].second)) {
      unioun(a, size, v[i + 1].first, v[i + 1].second);
      ans[i] = ans[i + 1] - n1 * n2;
    } else {
      ans[i] = ans[i + 1];
      // cout<<"anudeep"<<' '<<i;
    }
  }
  for (i = 0; i < m; i++) {
    cout << ans[i] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int maxn = 100000 + 5;
ll n1 = 0, n2 = 0;
ll root(ll arr[], ll a) {
  while (arr[a] != a) {
    arr[a] = arr[arr[a]];
    a = arr[a];
  }
  return a;
}
void unioun(ll arr[], ll size[], ll a, ll b) {
  int roota, rootb;
  roota = root(arr, a);
  rootb = root(arr, b);
  if (size[roota] < size[rootb]) {
    arr[roota] = arr[rootb];
    n1 = size[roota];
    n2 = size[rootb];
    size[rootb] += size[roota];
    size[roota] = 0;

  } else {
    arr[rootb] = arr[roota];
    n1 = size[roota];
    n2 = size[rootb];
    size[roota] += size[rootb];
  }
}
bool find(ll arr[], ll a, ll b) {
  int roota, rootb;
  roota = root(arr, a);
  rootb = root(arr, b);
  if (roota == rootb) {
    return true;
  }
  return false;
}
int main() {
  ll size[maxn], a[maxn], count[maxn], ans[maxn], i, x, y, n, m;
  cin >> n >> m;
  vector<pair<int, int>> v;
  for (i = 1; i < n + 1; i++) {
    a[i] = i;
    size[i] = 1;
    count[i] = 1;
  }
  //	v.push_back(make_pair(0,0));
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }
  // cout<<v[m-1].first<<' '<<v[m-1].second<<endl;
  ans[m - 1] = n * (n - 1) / 2;
  for (i = m - 2; i >= 0; i--) {
    if (!find(a, v[i + 1].first, v[i + 1].second)) {
      unioun(a, size, v[i + 1].first, v[i + 1].second);
      ans[i] = ans[i + 1] - n1 * n2;
    } else {
      ans[i] = ans[i + 1];
      // cout<<"anudeep"<<' '<<i;
    }
  }
  for (i = 0; i < m; i++) {
    cout << ans[i] << endl;
  }
}