Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define INF (1 << 30)
#define INFL (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define EPS 1e-10

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define all(a) (a).begin(), (a).end()
#define mp make_pair
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)

void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void yes() { cout << "yes" << endl; }
void no() { cout << "no" << endl; }
void Taka() { cout << "Takahashi" << endl; }
void Aoki() { cout << "Aoki" << endl; }
void digit(int d) { cout << fixed << setprecision(d); }
void IOup() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int n, m, a[50], b[50];
int v[51][51];

bool warshall() {
  int t[50][50];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      t[i][j] = v[i][j];
      if (t[i][j] == 0)
        t[i][j] = 100;
    }
  }

  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (t[i][j] == 100)
        return true;
    }
  }
  return false;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    v[a[i]][b[i]] = 1;
    v[b[i]][a[i]] = 1;
  }

  int ans = 0;
  for (int i = 0; i < m; ++i) {
    v[a[i]][b[i]] = v[b[i]][a[i]] = 0;
    if (warshall())
      ans++;
    v[a[i]][b[i]] = v[b[i]][a[i]] = 1;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define INF (1 << 30)
#define INFL (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define EPS 1e-10

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define all(a) (a).begin(), (a).end()
#define mp make_pair
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)

void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void yes() { cout << "yes" << endl; }
void no() { cout << "no" << endl; }
void Taka() { cout << "Takahashi" << endl; }
void Aoki() { cout << "Aoki" << endl; }
void digit(int d) { cout << fixed << setprecision(d); }
void IOup() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int n, m, a[50], b[50];
int v[51][51];

bool warshall() {
  int t[51][51];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      t[i][j] = v[i][j];
      if (t[i][j] == 0)
        t[i][j] = 100;
    }
  }

  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (t[i][j] == 100)
        return true;
    }
  }
  return false;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    v[a[i]][b[i]] = 1;
    v[b[i]][a[i]] = 1;
  }

  int ans = 0;
  for (int i = 0; i < m; ++i) {
    v[a[i]][b[i]] = v[b[i]][a[i]] = 0;
    if (warshall())
      ans++;
    v[a[i]][b[i]] = v[b[i]][a[i]] = 1;
  }
  cout << ans << endl;

  return 0;
}