Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Graph {
  int n;
  vector<set<int>> edge;
  vector<int> reached;

public:
  Graph(int N) : n(N), edge(N), reached(N) {}
  void add(int i, int j) {
    edge[i].emplace(j);
    edge[j].emplace(i);
  }
  void remove(int i, int j) {
    edge[i].erase(j);
    edge[j].erase(i);
  }
  void dfs(int i = 0) {
    if (reached[i])
      return;
    reached[i] = true;
    for (auto &j : edge[i]) {
      dfs(j);
    }
  }
  bool connected() {
    fill(reached.begin(), reached.end(), false);
    dfs();
    bool res = true;
    REP(i, n) res *= reached[i];
    return res;
  }
};

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

  int N, M;
  cin >> N >> M;
  Graph graph(N);
  vector<int> a(N), b(N);
  REP(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    graph.add(a[i], b[i]);
  }
  int res = 0;
  REP(i, M) {
    graph.remove(a[i], b[i]);
    if (!graph.connected())
      ++res;
    graph.add(a[i], b[i]);
  }
  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Graph {
  int n;
  vector<set<int>> edge;
  vector<int> reached;

public:
  Graph(int N) : n(N), edge(N), reached(N) {}
  void add(int i, int j) {
    edge[i].emplace(j);
    edge[j].emplace(i);
  }
  void remove(int i, int j) {
    edge[i].erase(j);
    edge[j].erase(i);
  }
  void dfs(int i = 0) {
    if (reached[i])
      return;
    reached[i] = true;
    for (auto &j : edge[i]) {
      dfs(j);
    }
  }
  bool connected() {
    fill(reached.begin(), reached.end(), false);
    dfs();
    bool res = true;
    REP(i, n) res *= reached[i];
    return res;
  }
};

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

  int N, M;
  cin >> N >> M;
  Graph graph(N);
  vector<int> a(M), b(M);
  REP(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    graph.add(a[i], b[i]);
  }
  int res = 0;
  REP(i, M) {
    graph.remove(a[i], b[i]);
    if (!graph.connected())
      ++res;
    graph.add(a[i], b[i]);
  }
  cout << res << endl;

  return 0;
}