prithivMLmods/Coder-Stat huggingface dataset:
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll tmp, n, m, i, j;
string str[51];

void check(ll a, ll b) {
  ll x[] = {-1, 0, 1};
  for (ll i = -1; i < 2; i++)
    for (ll j = -1; j < 2; j++) {
      if ((i || j) && (i + a >= 0 && i + a < n) && (j + b >= 0 && j + b < m))
        if (str[i + a][j + b] == '#')
          tmp++;
    }
}

int main() {
  // Create graphs given in above diagrams
  ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  cin >> n >> m;
  for (i = 0; i < n; i++)
    cin >> str[i];
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (str[i][j] != '.')
        continue;
      tmp = 0;
      check(i, j);
      str[i][j] = '0' + tmp;
    }
    cout << str[i] << "\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll tmp, n, m, i, j;
string str[51];

void check(ll a, ll b) {
  ll x[] = {-1, 0, 1};
  for (ll i = -1; i < 2; i++)
    for (ll j = -1; j < 2; j++) {
      if ((i || j) && (i + a >= 0 && i + a < n) && (j + b >= 0 && j + b < m))
        if (str[i + a][j + b] == '#')
          tmp++;
    }
}

int main() {

  cin >> n >> m;
  for (i = 0; i < n; i++)
    cin >> str[i];
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (str[i][j] != '.')
        continue;
      tmp = 0;
      check(i, j);
      str[i][j] = '0' + tmp;
    }
    cout << str[i] << "\n";
  }
  return 0;
}
Problem p03574 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;
  string S[W];
  for (int i = 0; i < H; ++i)
    cin >> S[i];

  //*def vector
  const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

  int num;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      // each cell
      if (S[i][j] == '#')
        continue;

      // count
      num = 0;
      // with vector
      for (int d = 0; d < 8; ++d) {
        const int ni = i + dy[d];
        const int nj = j + dx[d];

        if (ni < 0 || H <= ni)
          continue;
        if (nj < 0 || W <= nj)
          continue;
        if (S[ni][nj] == '#')
          num++;
      }
      S[i][j] = char(num + '0');
      // for(int k = i-1; k <= i+1; ++k){
      //     if(k < 0 || k >= H) continue;
      //     for(int l = j-1; l <= j+1; ++l){
      //         if(l < 0 || l >= W) continue;
      //         if(S[k][l] == '#') num++;
      //     }
      // }
      // S[i][j] = '0' + num;
    }
  }
  for (int i = 0; i < H; ++i)
    cout << S[i] << endl;
}

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

int main() {
  int H, W;
  cin >> H >> W;
  string S[50];
  for (int i = 0; i < H; ++i)
    cin >> S[i];

  //*def vector
  const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

  int num;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      // each cell
      if (S[i][j] == '#')
        continue;

      // count
      num = 0;
      // with vector
      for (int d = 0; d < 8; ++d) {
        const int ni = i + dy[d];
        const int nj = j + dx[d];

        if (ni < 0 || H <= ni)
          continue;
        if (nj < 0 || W <= nj)
          continue;
        if (S[ni][nj] == '#')
          num++;
      }
      S[i][j] = char(num + '0');
      // for(int k = i-1; k <= i+1; ++k){
      //     if(k < 0 || k >= H) continue;
      //     for(int l = j-1; l <= j+1; ++l){
      //         if(l < 0 || l >= W) continue;
      //         if(S[k][l] == '#') num++;
      //     }
      // }
      // S[i][j] = '0' + num;
    }
  }
  for (int i = 0; i < H; ++i)
    cout << S[i] << endl;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef vector<pair<ll, ll>> Q;
typedef pair<int, int> P;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
const ll inf = 1e9 + 7;
ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }
ll lcm(ll a, ll b) { return a / (gcd(a, b)) * b; }

int main() {
  int h, w;
  cin >> h >> w;

  char s[3 + 2][5 + 2];
  rep(i, 1, h + 1) rep(j, 1, w + 1) cin >> s[i][j];

  rep(i, 0, w + 2) s[0][i] = '.', s[h + 1][i] = '.';
  rep(i, 1, h + 1) s[i][0] = '.', s[i][w + 1] = '.';

  rep(i, 1, h + 1) {
    rep(j, 1, w + 1) {
      int tmp = 0;
      if (s[i][j] == '#')
        cout << '#';
      else {
        rep(k, -1, 2) rep(l, -1, 2) {
          if (s[i + k][j + l] == '#')
            tmp++;
        }

        cout << tmp;
      }
    }
    cout << endl;
  }

  /*    rep(i,0,h+2){
        rep(j,0,w+2) cout<<s[i][j];

        cout<<endl;
      }
  */
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef vector<pair<ll, ll>> Q;
typedef pair<int, int> P;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
const ll inf = 1e9 + 7;
ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }
ll lcm(ll a, ll b) { return a / (gcd(a, b)) * b; }

int main() {
  int h, w;
  cin >> h >> w;

  char s[h + 2][w + 2];
  rep(i, 1, h + 1) rep(j, 1, w + 1) cin >> s[i][j];

  rep(i, 0, w + 2) s[0][i] = '.', s[h + 1][i] = '.';
  rep(i, 1, h + 1) s[i][0] = '.', s[i][w + 1] = '.';

  rep(i, 1, h + 1) {
    rep(j, 1, w + 1) {
      int tmp = 0;
      if (s[i][j] == '#')
        cout << '#';
      else {
        rep(k, -1, 2) rep(l, -1, 2) {
          if (s[i + k][j + l] == '#')
            tmp++;
        }

        cout << tmp;
      }
    }
    cout << endl;
  }

  /*    rep(i,0,h+2){
        rep(j,0,w+2) cout<<s[i][j];

        cout<<endl;
      }
  */
}
Problem p03574 (C++)

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

string s[52];

int main(void) {
  int h, w;
  cin >> h >> w;
  for (int j = 0; j <= w + 1; j++)
    s[0] += '.';
  for (int i = 0; i < h; i++) {
    cin >> s[i];
    s[i] = '.' + s[i] + '.';
  }
  s[h + 1] = s[0];

  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (s[i][j] == '.') {
        int cnt = 0;
        for (int k = -1; k < 2; k++)
          for (int l = -1; l < 2; l++) {
            if (s[i + k][j + l] == '#')
              cnt++;
          }
        s[i][j] = '0' + cnt;
      }
    }

  for (int i = 1; i <= h; i++)
    cout << s[i].substr(1, w) << endl;
  return 0;
}

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

string s[52];

int main(void) {
  int h, w;
  cin >> h >> w;
  for (int j = 0; j <= w + 1; j++)
    s[0] += '.';
  for (int i = 1; i <= h; i++) {
    cin >> s[i];
    s[i] = '.' + s[i] + '.';
  }
  s[h + 1] = s[0];

  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (s[i][j] == '.') {
        int cnt = 0;
        for (int k = -1; k < 2; k++)
          for (int l = -1; l < 2; l++) {
            if (s[i + k][j + l] == '#')
              cnt++;
          }
        s[i][j] = '0' + cnt;
      }
    }

  for (int i = 1; i <= h; i++)
    cout << s[i].substr(1, w) << endl;
  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

int main(void) {
  int H, W;
  cin >> H >> W;
  string S[50];
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  int a[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
  int b[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        continue;
      }

      int num = 0;
      for (int t = 0; t < 8; t++) {
        const int ai = i + a[t];
        const int bj = j + b[t];

        if (ai < 0 || ai > H) {
          continue;
        }
        if (bj < 0 || bj > W) {
          continue;
        }
        if (S[ai][bj] == '#') {
          num++;
        }
      }
      S[i][j] = char(num + '0');
    }
  }

  for (int i = 0; i < H; i++) {
    cout << S[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int main(void) {
  int H, W;
  cin >> H >> W;

  string S[60];
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  int a[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
  int b[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        continue;
      }

      int num = 0;
      for (int t = 0; t < 8; t++) {
        const int ai = i + a[t];
        const int bj = j + b[t];

        if (ai < 0 || ai > H) {
          continue;
        }
        if (bj < 0 || bj > W) {
          continue;
        }
        if (S[ai][bj] == '#') {
          num++;
        }
      }
      S[i][j] = char(num + '0');
    }
  }

  for (int i = 0; i < H; i++) {
    cout << S[i] << endl;
  }
  return 0;
}
Problem p03574 (C++)

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

vector<int> dx = {1, 1, 0, -1, -1, -1, 0, 1};
vector<int> dy = {0, 1, 1, 1, 0, -1, -1, -1};

int main() {
  int h, w;
  cin >> h >> w;
  // 入力されるマップの外側を'.'で埋めておく
  vector<vector<char>> data(h + 2, vector<char>(w + 2, '.'));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> data[i][j];
    }
  }

  for (int i = 0; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      int cnt = 0;
      if (data[i][j] == '#')
        cout << '#';
      else {
        // 周り8マスを調べる
        for (int k = 0; k < 8; k++) {
          if (data[i + dx[k]][j + dy[k]] == '#')
            cnt++;
        }
        cout << cnt;
      }
    }
    cout << endl;
  }
}

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

vector<int> dx = {1, 1, 0, -1, -1, -1, 0, 1};
vector<int> dy = {0, 1, 1, 1, 0, -1, -1, -1};

int main() {
  int h, w;
  cin >> h >> w;
  // 入力されるマップの外側を'.'で埋めておく
  vector<vector<char>> data(h + 2, vector<char>(w + 2, '.'));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> data[i][j];
    }
  }

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      int cnt = 0;
      if (data[i][j] == '#')
        cout << '#';
      else {
        // 周り8マスを調べる
        for (int k = 0; k < 8; k++) {
          if (data[i + dx[k]][j + dy[k]] == '#')
            cnt++;
        }
        cout << cnt;
      }
    }
    cout << endl;
  }
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n, m, cnt, f, ans, a[10000], b[10000], flag[100];
queue<int> que;

int main() {
  cin >> n >> m;
  vector<vector<int>> Graph(m, vector<int>());
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    Graph[a[i]].push_back(b[i]);
    Graph[b[i]].push_back(a[i]);
  }
  for (int i = 0; i < m; i++) {
    que.push(0);
    flag[0] = 1;
    cnt = n - 1;
    while (!que.empty()) {
      f = que.front();
      que.pop();
      for (auto j : Graph[f]) {
        if (!flag[j] && !(a[i] == f && b[i] == j) &&
            !(a[i] == j && b[i] == f)) {
          flag[j] = 1;
          que.push(j);
          cnt--;
        }
      }
    }
    if (cnt == 0)
      ans++;
    for (int i = 0; i < n; i++)
      flag[i] = 0;
  }
  cout << m - ans << endl;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n, m, cnt, f, ans, a[10000], b[10000], flag[100];
queue<int> que;

int main() {
  cin >> n >> m;
  vector<vector<int>> Graph(n, vector<int>());
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    Graph[a[i]].push_back(b[i]);
    Graph[b[i]].push_back(a[i]);
  }
  for (int i = 0; i < m; i++) {
    que.push(0);
    flag[0] = 1;
    cnt = n - 1;
    while (!que.empty()) {
      f = que.front();
      que.pop();
      for (auto j : Graph[f]) {
        if (!flag[j] && !(a[i] == f && b[i] == j) &&
            !(a[i] == j && b[i] == f)) {
          flag[j] = 1;
          que.push(j);
          cnt--;
        }
      }
    }
    if (cnt == 0)
      ans++;
    for (int i = 0; i < n; i++)
      flag[i] = 0;
  }
  cout << m - ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define G_MAX 50
int graph[G_MAX + 1][G_MAX + 1];
int visit[G_MAX + 1];
int N, M;

void dfs(int n); // 連結かどうかを調べる深さ優先探索

int main(void) {
  vector<int> a, b;
  int sub;
  int cnt = 0;

  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> sub;
    a.push_back(sub);
    cin >> sub;
    b.push_back(sub);
  }

  // 隣接行列の作成
  for (int i = 0; i < M; i++) {
    graph[a[i]][b[i]] = 1;
    graph[b[i]][a[i]] = 1;
  }

  for (int i = 0; i < M; i++) {
    graph[a[i]][b[i]] = 0;
    graph[b[i]][a[i]] = 0; // 辺を取り除いてそれが橋であったかを調べる

    for (int j = 1; j <= N; j++) {
      visit[j] = 0;
    }

    dfs(1);

    bool bridge = false;
    for (int j = 1; j <= N; j++) {
      if (visit[j] == 0)
        bridge = true;
    }
    if (bridge)
      cnt++;

    graph[a[i]][b[i]] = 1;
    graph[b[i]][a[i]] = 1; // 処理が終われば元に戻す
  }

  cout << cnt << endl;
}

void dfs(
    int n) { // ある点から始めて、連結ならば訪問済みフラグを立てる　それができなければ非連結と判定
  visit[n] = 1;

  for (int x = 1; x <= N; x++) {
    if (graph[n][x] == 0)
      continue;
    if (visit[x] == 1)
      continue;
    dfs(x);
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define G_MAX 50
int graph[G_MAX + 1][G_MAX + 1];
int visit[G_MAX + 1];
int N, M;

void dfs(int n); // 連結かどうかを調べる深さ優先探索

int main(void) {
  vector<int> a, b;
  int sub;
  int cnt = 0;

  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> sub;
    a.push_back(sub);
    cin >> sub;
    b.push_back(sub);
  }

  // 隣接行列の作成
  for (int i = 0; i < M; i++) {
    graph[a[i]][b[i]] = 1;
    graph[b[i]][a[i]] = 1;
  }

  for (int i = 0; i < M; i++) {
    graph[a[i]][b[i]] = 0;
    graph[b[i]][a[i]] = 0; // 辺を取り除いてそれが橋であったかを調べる

    for (int j = 1; j <= N; j++) {
      visit[j] = 0;
    }

    dfs(1);

    bool bridge = false;
    for (int j = 1; j <= N; j++) {
      if (visit[j] == 0)
        bridge = true;
    }
    if (bridge)
      cnt++;

    graph[a[i]][b[i]] = 1;
    graph[b[i]][a[i]] = 1; // 処理が終われば元に戻す
  }

  cout << cnt << endl;
}

void dfs(
    int n) { // ある点から始めて、連結ならば訪問済みフラグを立てる　それができなければ非連結と判定
  visit[n] = 1;

  for (int x = 1; x <= N; x++) {
    if (graph[n][x] == 0)
      continue;
    if (visit[x] == 1)
      continue;
    dfs(x);
  }
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
const int MAX_N = 60;
int par[MAX_N];  // 親
int pank[MAX_N]; // 木の深さ

// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    pank[i] = 0;
  }
}

// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// xとyの属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (pank[x] < pank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (pank[x] == pank[y])
      pank[x]++;
  }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> p(n);
  int res = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    p[i] = {a, b};
  }
  rep(i, m) {
    init(n);
    bool ok = true;
    rep(j, m) {
      if (i == j)
        continue;
      int a = p[j].first, b = p[j].second;
      unite(a, b);
    }
    rep(j, n) rep(k, n) if (!same(j, k)) ok = false;
    if (!ok)
      res++;
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
const int MAX_N = 60;
int par[MAX_N];  // 親
int pank[MAX_N]; // 木の深さ

// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    pank[i] = 0;
  }
}

// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// xとyの属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (pank[x] < pank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (pank[x] == pank[y])
      pank[x]++;
  }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> p(m);
  int res = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    p[i] = {a, b};
  }
  rep(i, m) {
    init(n);
    bool ok = true;
    rep(j, m) {
      if (i == j)
        continue;
      int a = p[j].first, b = p[j].second;
      unite(a, b);
    }
    rep(j, n) rep(k, n) if (!same(j, k)) ok = false;
    if (!ok)
      res++;
  }
  cout << res << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> x(N);
  vector<pair<int, int>> y(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    x[a].push_back(b);
    x[b].push_back(a);
    y[i] = {a, b};
  }
  int ans = 0;
  for (int i = 0; i < M; i++) {
    vector<bool> k(N, 0);
    queue<int> z;
    z.push(0);
    while (z.size() != 0) {
      int l = z.front();
      k[l] = 1;
      z.pop();
      for (int j = 0; j < x[l].size(); j++) {
        if (l == y[i].first && x[l][j] == y[i].second)
          continue;
        if (l == y[i].second && x[l][j] == y[i].first)
          continue;
        if (k[x[l][j]] == true)
          continue;
        z.push(x[l][j]);
      }
    }
    for (int j = 0; j < N; j++) {
      if (k[j] == false) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> x(N);
  vector<pair<int, int>> y(M);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    x[a].push_back(b);
    x[b].push_back(a);
    y[i] = {a, b};
  }
  int ans = 0;
  for (int i = 0; i < M; i++) {
    vector<bool> k(N, 0);
    queue<int> z;
    z.push(0);
    while (z.size() != 0) {
      int l = z.front();
      k[l] = 1;
      z.pop();
      for (int j = 0; j < x[l].size(); j++) {
        if (l == y[i].first && x[l][j] == y[i].second)
          continue;
        if (l == y[i].second && x[l][j] == y[i].first)
          continue;
        if (k[x[l][j]] == true)
          continue;
        z.push(x[l][j]);
      }
    }
    for (int j = 0; j < N; j++) {
      if (k[j] == false) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const ll MOD = 1e9 + 7;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int main() {
  ll n, m, p;
  cin >> n >> m;
  ll ans = 0;
  vector<vector<ll>> a(n + 1);
  vector<vector<bool>> b(n + 1, vector<bool>(n + 1, false));
  vector<ll> x(n + 1), y(n + 1);
  vector<bool> v(n + 1, false);

  rep(i, m) {
    cin >> x[i] >> y[i];
    a[x[i]].push_back(y[i]);
    a[y[i]].push_back(x[i]);
    b[x[i]][y[i]] = true;
    b[y[i]][x[i]] = true;
  }

  queue<ll> q;

  rep(i, m) {
    b[x[i]][y[i]] = false;
    b[y[i]][x[i]] = false;
    q.push(1);

    while (!q.empty()) {
      p = q.front();
      q.pop();

      rep(j, a[p].size()) {
        if (b[p][a[p][j]] && !v[a[p][j]]) {
          v[a[p][j]] = true;
          q.push(a[p][j]);
        }
      }
    }

    for (int j = 1; j <= n; j++) {
      if (!v[j]) {
        ans++;
        break;
      }
    }
    rep(j, n + 1) v[j] = false;

    b[x[i]][y[i]] = true;
    b[y[i]][x[i]] = true;
  }

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const ll MOD = 1e9 + 7;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int main() {
  ll n, m, p;
  cin >> n >> m;
  ll ans = 0;
  vector<vector<ll>> a(51);
  vector<vector<bool>> b(51, vector<bool>(51, false));
  vector<ll> x(51), y(51);
  vector<bool> v(51, false);

  rep(i, m) {
    cin >> x[i] >> y[i];
    a[x[i]].push_back(y[i]);
    a[y[i]].push_back(x[i]);
    b[x[i]][y[i]] = true;
    b[y[i]][x[i]] = true;
  }

  queue<ll> q;

  rep(i, m) {
    b[x[i]][y[i]] = false;
    b[y[i]][x[i]] = false;
    q.push(1);

    while (!q.empty()) {
      p = q.front();
      q.pop();

      rep(j, a[p].size()) {
        if (b[p][a[p][j]] && !v[a[p][j]]) {
          v[a[p][j]] = true;
          q.push(a[p][j]);
        }
      }
    }

    for (int j = 1; j <= n; j++) {
      if (!v[j]) {
        ans++;
        break;
      }
    }
    rep(j, n + 1) v[j] = false;

    b[x[i]][y[i]] = true;
    b[y[i]][x[i]] = true;
  }

  cout << ans;
}
Problem p03575 (C++)

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

using namespace std;
#define ll long long
const ll maxn = 55;
int n;                 // number of nodes
vector<int> adj[maxn]; // adjacency list of graph
int bridges = 0;
vector<bool> visited;
vector<int> tin, low;
int timer;

void dfs(int v, int p = -1) {
  visited[v] = true;
  tin[v] = low[v] = timer++;
  for (int to : adj[v]) {
    if (to == p)
      continue;
    if (visited[to]) {
      low[v] = min(low[v], tin[to]);
    } else {
      dfs(to, v);
      low[v] = min(low[v], low[to]);
      if (low[to] > tin[v])
        bridges++;
    }
  }
}

void find_bridges() {
  timer = 0;
  visited.assign(n, false);
  tin.assign(n, -1);
  low.assign(n, -1);
  for (int i = 0; i < n; ++i) {
    if (!visited[i])
      dfs(i);
  }
}
int main() {
  int m;
  cin >> n >> m;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  find_bridges();
  cout << bridges;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define ll long long
const ll maxn = 55;
int n;                 // number of nodes
vector<int> adj[maxn]; // adjacency list of graph
int bridges = 0;
vector<bool> visited;
vector<int> tin, low;
int timer;

void dfs(int v, int p = -1) {
  visited[v] = true;
  tin[v] = low[v] = timer++;
  for (int to : adj[v]) {
    if (to == p)
      continue;
    if (visited[to]) {
      low[v] = min(low[v], tin[to]);
    } else {
      dfs(to, v);
      low[v] = min(low[v], low[to]);
      if (low[to] > tin[v])
        bridges++;
    }
  }
}

void find_bridges() {
  timer = 0;
  visited.assign(maxn, false);
  tin.assign(maxn, -1);
  low.assign(maxn, -1);
  for (int i = 0; i < n; ++i) {
    if (!visited[i])
      dfs(i);
  }
}
int main() {
  int m;
  cin >> n >> m;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  find_bridges();
  cout << bridges;
}
Problem p03575 (C++)

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

const int INF = 1e9;
const int MAX = 10;
int n, m;
struct edge {
  int to;
  int cost;
};
vector<edge> G[MAX];
int D[MAX];

void dijkstra(int s, int x, int y) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  for (int i = 0; i < n; i++) {
    D[i] = INF;
  }
  D[s] = 0;
  q.push({0, s});

  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int now = p.second;
    if (D[now] < p.first)
      continue;
    for (int i = 0; i < G[now].size(); i++) {
      edge e = G[now][i];
      if ((now == x and G[now][i].to == y) or (now == y and G[now][i].to == x))
        continue;
      if (D[e.to] > D[now] + e.cost) {
        D[e.to] = D[now] + e.cost;
        q.push({D[e.to], e.to});
      }
    }
  }
}

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

  cin >> n >> m;

  vector<int> A(m), B(m);

  for (int i = 0; i < m; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    G[A[i]].push_back({B[i], 1});
    G[B[i]].push_back({A[i], 1});
  }

  int ans = 0;
  for (int i = 0; i < m; i++) {

    dijkstra(0, A[i], B[i]);

    for (int j = 0; j < n; j++) {
      if (D[j] == INF) {
        ans++;
        break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

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

const int INF = 1e9;
const int MAX = 55;
int n, m;
struct edge {
  int to;
  int cost;
};
vector<edge> G[MAX];
int D[MAX];

void dijkstra(int s, int x, int y) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  for (int i = 0; i < n; i++) {
    D[i] = INF;
  }
  D[s] = 0;
  q.push({0, s});

  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int now = p.second;
    if (D[now] < p.first)
      continue;
    for (int i = 0; i < G[now].size(); i++) {
      edge e = G[now][i];
      if ((now == x and G[now][i].to == y) or (now == y and G[now][i].to == x))
        continue;
      if (D[e.to] > D[now] + e.cost) {
        D[e.to] = D[now] + e.cost;
        q.push({D[e.to], e.to});
      }
    }
  }
}

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

  cin >> n >> m;

  vector<int> A(m), B(m);

  for (int i = 0; i < m; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    G[A[i]].push_back({B[i], 1});
    G[B[i]].push_back({A[i], 1});
  }

  int ans = 0;
  for (int i = 0; i < m; i++) {

    dijkstra(0, A[i], B[i]);

    for (int j = 0; j < n; j++) {
      if (D[j] == INF) {
        ans++;
        break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int m, n;
bool vi[100];
vector<int> g[50][50];
void dfs(int i, int u) {
  for (auto v : g[u][i]) {
    if (!vi[v]) {
      vi[v] = true;
      dfs(i, v);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    for (int j = 0; j < m; ++j) {
      if (j != i) {
        g[a][j].push_back(b);
        g[b][j].push_back(a);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < m; ++i) {
    memset(vi, false, sizeof(vi));
    vi[1] = true;
    dfs(i, 1);
    for (int j = 1; j <= n; ++j)
      if (!vi[j]) {
        ans++;
        break;
      }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int m, n;
bool vi[100];
vector<int> g[60][60];
void dfs(int i, int u) {
  for (auto v : g[u][i]) {
    if (!vi[v]) {
      vi[v] = true;
      dfs(i, v);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    for (int j = 0; j < m; ++j) {
      if (j != i) {
        g[a][j].push_back(b);
        g[b][j].push_back(a);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < m; ++i) {
    memset(vi, false, sizeof(vi));
    vi[1] = true;
    dfs(i, 1);
    for (int j = 1; j <= n; ++j)
      if (!vi[j]) {
        ans++;
        break;
      }
  }
  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (ll i = 0; i < N; ++i)
const ll INF = 1LL << 60;

ll N, M;
vector<vector<ll>> G(N + 10);
vector<ll> used(N + 10);

bool dfs(int cur, int start, int goal) {
  used[cur] = 1;
  int n = -1;
  for (auto it : G[cur]) {
    n = it;
    if (cur == start && n == goal)
      continue;
    if (n == goal)
      return true;
    if (used[n])
      continue;
    if (dfs(n, start, goal))
      return true;
  }
  return false;
}

int main() {
  cin >> N >> M;
  vector<ll> a(M), b(M);
  rep(i, M) cin >> a[i] >> b[i];
  rep(i, M) {
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  ll ans = M;
  rep(i, M) {
    fill(used.begin(), used.end(), 0);
    if (dfs(a[i], a[i], b[i]))
      ans--;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (ll i = 0; i < N; ++i)
const ll INF = 1LL << 60;

ll N, M;
vector<vector<ll>> G(55);
vector<ll> used(55);

bool dfs(int cur, int start, int goal) {
  used[cur] = 1;
  int n = -1;
  for (auto it : G[cur]) {
    n = it;
    if (cur == start && n == goal)
      continue;
    if (n == goal)
      return true;
    if (used[n])
      continue;
    if (dfs(n, start, goal))
      return true;
  }
  return false;
}

int main() {
  cin >> N >> M;
  vector<ll> a(M), b(M);
  rep(i, M) cin >> a[i] >> b[i];
  rep(i, M) {
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  ll ans = M;
  rep(i, M) {
    fill(used.begin(), used.end(), 0);
    if (dfs(a[i], a[i], b[i]))
      ans--;
  }
  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int N, M;

void dfs(int n, vector<vector<int>> &Graph, vector<bool> &seen) {
  seen[n] = true;
  for (auto x : Graph[n]) {
    if (!seen[x])
      dfs(x, Graph, seen);
  }
  return;
}

bool bridge(int n, vector<int> &a, vector<int> &b) {
  int cnt = 0;
  vector<bool> seen(N, false);
  vector<vector<int>> Graph(N);
  rep(i, M) {
    if (i == n)
      continue;
    Graph[a[i]].push_back(b[i]);
    Graph[b[i]].push_back(a[i]);
  }

  rep(i, N) if (!seen[i]) {
    ++cnt;
    dfs(i, Graph, seen);
  }
  if (cnt == 1)
    return false;
  else
    return true;
}

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

  int ans = 0;
  rep(i, M) if (bridge(i, a, b))++ ans;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int N, M;

void dfs(int n, vector<vector<int>> &Graph, vector<bool> &seen) {
  seen[n] = true;
  for (auto x : Graph[n]) {
    if (!seen[x])
      dfs(x, Graph, seen);
  }
  return;
}

bool bridge(int n, vector<int> &a, vector<int> &b) {
  int cnt = 0;
  vector<bool> seen(N, false);
  vector<vector<int>> Graph(N);
  rep(i, M) {
    if (i == n)
      continue;
    Graph[a[i]].push_back(b[i]);
    Graph[b[i]].push_back(a[i]);
  }

  rep(i, N) if (!seen[i]) {
    ++cnt;
    dfs(i, Graph, seen);
  }
  if (cnt == 1)
    return false;
  else
    return true;
}

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

  int ans = 0;
  rep(i, M) if (bridge(i, a, b))++ ans;
  cout << ans << endl;
}
Problem p03575 (C++)

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

struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool same(int a, int b) { return root(a) == root(b); }

  void unite(int a, int b) {
    int x = root(a), y = root(b);
    if (x == y)
      return;
    if (par[y] < par[x])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
  }
};

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> a(n), b(n);
  for (int i = 0; i < m; i++) {
    cin >> a.at(i) >> b.at(i);
    a.at(i)--;
    b.at(i)--;
  }
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    UnionFind land(n);
    for (int j = 0; j < m; j++) {
      if (i == j)
        continue;
      land.unite(a[j], b[j]);
    }
    if (!land.same(a[i], b[i]))
      cnt += 1;
  }

  cout << cnt << endl;
}

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

struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool same(int a, int b) { return root(a) == root(b); }

  void unite(int a, int b) {
    int x = root(a), y = root(b);
    if (x == y)
      return;
    if (par[y] < par[x])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
  }
};

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> a(m), b(m);
  for (int i = 0; i < m; i++) {
    cin >> a.at(i) >> b.at(i);
    a.at(i)--;
    b.at(i)--;
  }
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    UnionFind land(n);
    for (int j = 0; j < m; j++) {
      if (i == j)
        continue;
      land.unite(a[j], b[j]);
    }
    if (!land.same(a[i], b[i]))
      cnt += 1;
  }

  cout << cnt << endl;
}
Problem p03575 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

struct UnionFind {
  vector<int> data;

  UnionFind(int n) {
    data.resize(n + 1);
    fill(data.begin(), data.end(), -1);
  }

  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    if (rx != ry) {
      if (data[ry] < data[rx])
        swap(rx, ry);

      data[rx] += data[ry];
      data[ry] = rx;
    }
  }

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

  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  rep(i, m) cin >> a[i] >> b[i];
  int result = 0;
  rep(i, m) {
    UnionFind uf(n);
    rep(j, m) if (i != j) uf.unite(a[j], b[j]);
    if (!uf.same(a[i], b[i]))
      result++;
  }
  cout << result << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

struct UnionFind {
  vector<int> data;

  UnionFind(int n) {
    data.resize(n + 1);
    fill(data.begin(), data.end(), -1);
  }

  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    if (rx != ry) {
      if (data[ry] < data[rx])
        swap(rx, ry);

      data[rx] += data[ry];
      data[ry] = rx;
    }
  }

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

  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  rep(i, m) cin >> a[i] >> b[i];
  int result = 0;
  rep(i, m) {
    UnionFind uf(n);
    rep(j, m) if (i != j) uf.unite(a[j], b[j]);
    if (!uf.same(a[i], b[i]))
      result++;
  }
  cout << result << endl;

  return 0;
}
Problem p03575 (C++)

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

struct UnionFind {
  vector<int> par;

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

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

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

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  int a[n], b[n];
  for (int i = 0; i < m; ++i)
    cin >> a[i] >> b[i];
  for (int i = 0; i < m; ++i) {
    UnionFind uf(n);
    for (int j = 0; j < m; ++j) {
      if (i != j)
        uf.unite(a[j] - 1, b[j] - 1);
    }
    int grope_num = 0;
    for (int j = 0; j < n; ++j) {
      if (uf.root(j) == j)
        grope_num++;
    }
    if (grope_num != 1)
      ans++;
  }

  cout << ans << '\n';

  return 0;
}


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

struct UnionFind {
  vector<int> par;

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

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

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

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  int a[m], b[m];
  for (int i = 0; i < m; ++i)
    cin >> a[i] >> b[i];
  for (int i = 0; i < m; ++i) {
    UnionFind uf(n);
    for (int j = 0; j < m; ++j) {
      if (i != j)
        uf.unite(a[j] - 1, b[j] - 1);
    }
    int grope_num = 0;
    for (int j = 0; j < n; ++j) {
      if (uf.root(j) == j)
        grope_num++;
    }
    if (grope_num != 1)
      ans++;
  }

  cout << ans << '\n';

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
bool bfs(vector<vector<int>> &hen) {
  int n = hen.size();
  vector<int> tu(n);
  tu.clear();
  tu[0] = 1;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int town = q.front();
    q.pop();
    for (int i = 0; i < n; i++)
      if (hen[town][i] == 1 && tu[i] == 0)
        q.push(i), tu[i] = 1;
  }
  for (int i = 0; i < n; i++)
    if (!tu[i])
      return 1;
  return 0;
}
int main() {
  int ans = 0;
  int n, m;
  cin >> n >> m;
  vector<vector<int>> hen(n);
  for (int i = 0; i < n; i++)
    hen[i].resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    hen[a][b] = hen[b][a] = 1;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < i; j++) {
      if (!hen[i][j])
        continue;
      hen[i][j] = hen[j][i] = 0;
      ans += bfs(hen);
      hen[i][j] = hen[j][i] = 1;
    }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
bool bfs(vector<vector<int>> &hen) {
  int n = hen.size();
  vector<int> tu(n);
  tu.clear();
  tu[0] = 1;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int town = q.front();
    q.pop();
    for (int i = 0; i < n; i++)
      if (hen[town][i] == 1 && tu[i] == 0)
        q.push(i), tu[i] = 1;
  }
  for (int i = 0; i < n; i++)
    if (!tu[i])
      return 1;
  return 0;
}
int main() {
  int ans = 0;
  int n, m;
  cin >> n >> m;
  vector<vector<int>> hen(n);
  for (int i = 0; i < n; i++)
    hen[i].resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    hen[a][b] = hen[b][a] = 1;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < i; j++) {
      if (!hen[i][j])
        continue;
      hen[i][j] = hen[j][i] = 0;
      ans += bfs(hen);
      hen[i][j] = hen[j][i] = 1;
    }
  cout << ans;
}
Problem p03575 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;

  int A[n], B[n];
  for (int i = 0; i < m; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  int cnt = 0;
  for (int c = 0; c < m; c++) {
    int G[n][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          G[i][j] = 0;
        else
          G[i][j] = 1e9;
      }
    }

    for (int i = 0; i < m; i++) {
      if (i == c)
        continue;
      else {
        G[A[i]][B[i]] = 1;
        G[B[i]][A[i]] = 1;
      }
    }

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

    bool f = false;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (G[i][j] == 1e9) {
          f = true;
          break;
        }
      }
      if (f)
        break;
    }
    if (f)
      cnt++;
  }
  cout << cnt << endl;

  return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;

  int A[m], B[m];
  for (int i = 0; i < m; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  int cnt = 0;
  for (int c = 0; c < m; c++) {
    int G[n][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          G[i][j] = 0;
        else
          G[i][j] = 1e9;
      }
    }

    for (int i = 0; i < m; i++) {
      if (i == c)
        continue;
      else {
        G[A[i]][B[i]] = 1;
        G[B[i]][A[i]] = 1;
      }
    }

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

    bool f = false;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (G[i][j] == 1e9) {
          f = true;
          break;
        }
      }
      if (f)
        break;
    }
    if (f)
      cnt++;
  }
  cout << cnt << endl;

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define INF 1e9
#define MOD 1e9 + 7
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, a, n) for (int i = a; i < n; i++)
#define all(in) in.begin(), in.end()

const int MAX_N = 50;
int N, M;
bool Graph[MAX_N + 1][MAX_N + 1];
bool visited[MAX_N + 1];
int dfs(int v) {
  visited[v] = true;
  for (int v2 = 0; v2 < N; v2++) {
    if (Graph[v][v2] == false)
      continue;
    if (visited[v2] == true)
      continue;
    dfs(v2);
  }
}
int main() {
  cin >> N >> M;
  vector<int> a(M), b(M);
  rep(i, N) {
    cin >> a[i] >> b[i];
    a[i]--, b[i]--;
    Graph[a[i]][b[i]] = Graph[b[i]][a[i]] = true;
  }
  int ans = 0;
  for (int i = 0; i < M; i++) {
    Graph[a[i]][b[i]] = Graph[b[i]][a[i]] = false;
    rep(i, N) visited[i] = false;
    dfs(0);
    bool bridge = false;
    rep(i, N) if (visited[i] == false) bridge = true;
    if (bridge)
      ans++;
    Graph[a[i]][b[i]] = Graph[b[i]][a[i]] = true;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define INF 1e9
#define MOD 1e9 + 7
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, a, n) for (int i = a; i < n; i++)
#define all(in) in.begin(), in.end()

const int MAX_N = 50;
int N, M;
bool Graph[MAX_N + 1][MAX_N + 1];
bool visited[MAX_N + 1];
int dfs(int v) {
  visited[v] = true;
  for (int v2 = 0; v2 < N; v2++) {
    if (Graph[v][v2] == false)
      continue;
    if (visited[v2] == true)
      continue;
    dfs(v2);
  }
}
int main() {
  cin >> N >> M;
  vector<int> a(M), b(M);
  rep(i, M) {
    cin >> a[i] >> b[i];
    a[i]--, b[i]--;
    Graph[a[i]][b[i]] = Graph[b[i]][a[i]] = true;
  }
  int ans = 0;
  for (int i = 0; i < M; i++) {
    Graph[a[i]][b[i]] = Graph[b[i]][a[i]] = false;
    rep(i, N) visited[i] = false;
    dfs(0);
    bool bridge = false;
    rep(i, N) if (visited[i] == false) bridge = true;
    if (bridge)
      ans++;
    Graph[a[i]][b[i]] = Graph[b[i]][a[i]] = true;
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lint long long
// #define P pair<int, int>

int main() {
  int N, M;
  cin >> N >> M;

  vector<vector<int>> G(51);
  int a[N], b[N];

  for (int i = 0; i < M; ++i) {
    cin >> a[i] >> b[i];

    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }

  int ans = 0;

  for (int i = 0; i < M; ++i) {
    //        cerr << i << " : ";
    bool tf[N + 1];
    memset(tf, false, sizeof tf);
    queue<int> que;
    que.push(1);

    while (!que.empty()) {
      int p = que.front();
      que.pop();
      for (int j = 0; j < G[p].size(); ++j) {
        int e = G[p][j];
        if (tf[e]) {
          continue;
        } else {
          if ((p == a[i] && e == b[i]) || (p == b[i] && e == a[i])) {
            continue;
          }
          tf[e] = true;
          que.push(e);
          //                    cerr << e << " ";
        }
      }
    }
    //        cerr << endl;

    for (int i = 1; i <= N; ++i) {
      if (!tf[i]) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lint long long
// #define P pair<int, int>

int main() {
  int N, M;
  cin >> N >> M;

  vector<vector<int>> G(51);
  int a[M], b[M];

  for (int i = 0; i < M; ++i) {
    cin >> a[i] >> b[i];

    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }

  int ans = 0;

  for (int i = 0; i < M; ++i) {
    //        cerr << i << " : ";
    bool tf[N + 1];
    memset(tf, false, sizeof tf);
    queue<int> que;
    que.push(1);

    while (!que.empty()) {
      int p = que.front();
      que.pop();
      for (int j = 0; j < G[p].size(); ++j) {
        int e = G[p][j];
        if (tf[e]) {
          continue;
        } else {
          if ((p == a[i] && e == b[i]) || (p == b[i] && e == a[i])) {
            continue;
          }
          tf[e] = true;
          que.push(e);
          //                    cerr << e << " ";
        }
      }
    }
    //        cerr << endl;

    for (int i = 1; i <= N; ++i) {
      if (!tf[i]) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}