Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a), i##_len = (b); i < i##_len; ++i)
#define MSVC_UNKO(x) x
#define rep(...)                                                               \
  MSVC_UNKO(_overload3(__VA_ARGS__, repi, _rep, _rep)(__VA_ARGS__))
#define all(c) c.begin(), c.end()
#define mp make_pair
#define write(x) cout << (x) << "\n"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
template <class T> using vv = vector<vector<T>>;
template <class T> vv<T> vvec(size_t n, size_t m, T v) {
  return vv<T>(n, vector<T>(m, v));
}
template <class T> bool chmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <class T> bool chmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
constexpr int INF = 1 << 28, MOD = int(1e9) + 7;
constexpr ll LINF = 1ll << 60;
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  };
} aaaa;

struct UnionFind {
  vector<int> parent;
  vector<int> rank;
  int operator[](int x) { return find(x); }
  void init(int size) {
    parent.assign(size, -1);
    rank.assign(size, 0);
  }
  int find(int x) { return parent[x] < 0 ? x : (parent[x] = find(parent[x])); }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -parent[find(x)]; }
  bool unite(int x, int y) {
    if ((x = find(x)) == (y = find(y)))
      return 0;
    return rank[x] = max(rank[x], rank[y] + 1), parent[x] += parent[y],
           parent[y] = x, 1;
  }
  bool uniteByRank(int x, int y) {
    if ((x = find(x)) == (y = find(y)))
      return 0;
    else if (rank[x] < rank[y])
      swap(x, y);
    else if (rank[x] == rank[y])
      rank[x]++;
    return parent[x] += parent[y], parent[y] = x, 1;
  }
  bool uniteBySize(int x, int y) {
    if ((x = find(x)) == (y = find(y)))
      return 0;
    else if (size(x) < size(y))
      swap(x, y);
    return rank[x] = max(rank[x], rank[y] + 1), parent[x] += parent[y],
           parent[y] = x, 1;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vi a(N), b(N);
  rep(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  UnionFind uf;
  int ans = 0;
  rep(i, M) {
    uf.init(N);
    rep(j, M) {
      if (i == j)
        continue;
      uf.uniteByRank(a[j], b[j]);
    }

    bool isConnected = true;
    int group = uf[0];
    rep(n, N) {
      if (uf[n] != group)
        isConnected = false;
    }
    if (!isConnected)
      ans++;
  }

  write(ans);
}

Fixed submission:
#include "bits/stdc++.h"
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a), i##_len = (b); i < i##_len; ++i)
#define MSVC_UNKO(x) x
#define rep(...)                                                               \
  MSVC_UNKO(_overload3(__VA_ARGS__, repi, _rep, _rep)(__VA_ARGS__))
#define all(c) c.begin(), c.end()
#define mp make_pair
#define write(x) cout << (x) << "\n"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
template <class T> using vv = vector<vector<T>>;
template <class T> vv<T> vvec(size_t n, size_t m, T v) {
  return vv<T>(n, vector<T>(m, v));
}
template <class T> bool chmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <class T> bool chmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
constexpr int INF = 1 << 28, MOD = int(1e9) + 7;
constexpr ll LINF = 1ll << 60;
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  };
} aaaa;

struct UnionFind {
  vector<int> parent;
  vector<int> rank;
  int operator[](int x) { return find(x); }
  void init(int size) {
    parent.assign(size, -1);
    rank.assign(size, 0);
  }
  int find(int x) { return parent[x] < 0 ? x : (parent[x] = find(parent[x])); }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -parent[find(x)]; }
  bool unite(int x, int y) {
    if ((x = find(x)) == (y = find(y)))
      return 0;
    return rank[x] = max(rank[x], rank[y] + 1), parent[x] += parent[y],
           parent[y] = x, 1;
  }
  bool uniteByRank(int x, int y) {
    if ((x = find(x)) == (y = find(y)))
      return 0;
    else if (rank[x] < rank[y])
      swap(x, y);
    else if (rank[x] == rank[y])
      rank[x]++;
    return parent[x] += parent[y], parent[y] = x, 1;
  }
  bool uniteBySize(int x, int y) {
    if ((x = find(x)) == (y = find(y)))
      return 0;
    else if (size(x) < size(y))
      swap(x, y);
    return rank[x] = max(rank[x], rank[y] + 1), parent[x] += parent[y],
           parent[y] = x, 1;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vi a(M), b(M);
  rep(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  UnionFind uf;
  int ans = 0;
  rep(i, M) {
    uf.init(N);
    rep(j, M) {
      if (i == j)
        continue;
      uf.uniteByRank(a[j], b[j]);
    }

    bool isConnected = true;
    int group = uf[0];
    rep(n, N) {
      if (uf[n] != group)
        isConnected = false;
    }
    if (!isConnected)
      ans++;
  }

  write(ans);
}