Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;
#define int long long
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define P pair<ll, ll>
#define sz(x) (ll) x.size()
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define VE vector<ll>
#define COUT(x) cout << (x) << endl
#define MA map<ll, ll>
#define SE set<ll>
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, VE, greater<ll>>
#define COUT(x) cout << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define EPS (1e-10)

const long long MOD = 1000000007;
// const long long MOD = 998244353;
const long long INF = 1LL << 60;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a * g / b;
}
bool prime(ll n) {
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}

const long double PI = 3.14159265359;
const int MAX_N = 100010;

bool seen[100];
void dfs(const vector<VE> &G, int v) {
  seen[v] = true;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v); // 再帰的に探索
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  VE a(n), b(n);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int ans = 0;
  rep(i, m) {
    vector<VE> G(n);
    rep(j, m) {
      if (i != j) {
        G[a[j]].push_back(b[j]);
        G[b[j]].push_back(a[j]);
      }
    }
    // 全頂点が訪問済みになるまで探索
    int count = 0;
    rep(j, n) seen[j] = false;
    for (int v = 0; v < n; ++v) {
      if (seen[v])
        continue; // v が探索済みだったらスルー
      dfs(G, v);  // v が未探索なら v を始点とした DFS を行う
      ++count;
    }
    if (count != 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;
#define int long long
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define P pair<ll, ll>
#define sz(x) (ll) x.size()
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define VE vector<ll>
#define COUT(x) cout << (x) << endl
#define MA map<ll, ll>
#define SE set<ll>
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, VE, greater<ll>>
#define COUT(x) cout << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define EPS (1e-10)

const long long MOD = 1000000007;
// const long long MOD = 998244353;
const long long INF = 1LL << 60;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a * g / b;
}
bool prime(ll n) {
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}

const long double PI = 3.14159265359;
const int MAX_N = 100010;

bool seen[100];
void dfs(const vector<VE> &G, int v) {
  seen[v] = true;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v); // 再帰的に探索
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  VE a(m), b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int ans = 0;
  rep(i, m) {
    vector<VE> G(n);
    rep(j, m) {
      if (i != j) {
        G[a[j]].push_back(b[j]);
        G[b[j]].push_back(a[j]);
      }
    }
    // 全頂点が訪問済みになるまで探索
    int count = 0;
    rep(j, n) seen[j] = false;
    for (int v = 0; v < n; ++v) {
      if (seen[v])
        continue; // v が探索済みだったらスルー
      dfs(G, v);  // v が未探索なら v を始点とした DFS を行う
      ++count;
    }
    if (count != 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}