Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

template <class Abel> struct UnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

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

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      ++rank[x];
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

// Union-Find解答
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> ed(n, vector<int>(2));

  rep(i, m) {
    cin >> ed[i][0] >> ed[i][1];
    ed[i][0]--;
    ed[i][1]--;
  }

  int ans = 0;
  rep(_, m) {
    bool jud = false;
    UnionFind<int> uf(n);
    rep(i, m) {
      if (i == _)
        continue;
      uf.merge(ed[i][0], ed[i][1], 0);
    }

    rep(i, n) {
      rep(j, n) {
        if (i == j)
          continue;

        if (!uf.issame(i, j)) {
          jud = true;
        }
      }
    }
    if (jud)
      ans++;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

template <class Abel> struct UnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

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

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      ++rank[x];
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

// Union-Find解答
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> ed(m, vector<int>(2));

  rep(i, m) {
    cin >> ed[i][0] >> ed[i][1];
    ed[i][0]--;
    ed[i][1]--;
  }

  int ans = 0;
  rep(_, m) {
    bool jud = false;
    UnionFind<int> uf(n);
    rep(i, m) {
      if (i == _)
        continue;
      uf.merge(ed[i][0], ed[i][1], 0);
    }

    rep(i, n) {
      rep(j, n) {
        if (i == j)
          continue;

        if (!uf.issame(i, j)) {
          jud = true;
        }
      }
    }
    if (jud)
      ans++;
  }

  cout << ans << endl;
}