Problem p03575 (C++)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

const int MAX_N = 500;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

struct UnionFind {
  int N;
  int par[MAX_N];

  UnionFind(int N) : N(N) {
    for (int i = 0; i < N; i++) {
      par[i] = i;
    }
  }

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

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

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

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> a(N), b(N);
  REP(i, N) cin >> a[i] >> b[i];

  ll res = 0;
  REP(i, M) {
    UnionFind uf(N);
    REP(j, M) {
      if (j == i)
        continue;
      uf.unite(a[j] - 1, b[j] - 1);
    }
    bool flag = false;
    /*
    REP(j, N){
        cout << uf.root(j) << " ";
    }
    cout << endl;
    */
    ll tmp = uf.root(0);
    REP(j, N) {
      if (tmp != uf.root(j))
        flag = true;
    }
    res += flag;
  }
  cout << res << endl;
  return 0;
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

const int MAX_N = 500;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

struct UnionFind {
  int N;
  int par[MAX_N];

  UnionFind(int N) : N(N) {
    for (int i = 0; i < N; i++) {
      par[i] = i;
    }
  }

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

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

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

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> a(M), b(M);
  REP(i, M) cin >> a[i] >> b[i];

  ll res = 0;
  REP(i, M) {
    UnionFind uf(N);
    REP(j, M) {
      if (j == i)
        continue;
      uf.unite(a[j] - 1, b[j] - 1);
    }
    bool flag = false;
    /*
    REP(j, N){
        cout << uf.root(j) << " ";
    }
    cout << endl;
    */
    ll tmp = uf.root(0);
    REP(j, N) {
      if (tmp != uf.root(j))
        flag = true;
    }
    res += flag;
  }
  cout << res << endl;
  return 0;
}