prithivMLmods/Coder-Stat huggingface dataset:
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;
    else
      return par[x] = root(par[x]);
  }

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

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

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

  vector<int> a(M), b(M);

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

  int ans = 0;
  for (int i = 0; i < M; i++) {
    UnionFind tree(N);

    for (int j = 0; j < M; j++) {
      if (j == i)
        continue;
      tree.unite(a[j], b[j]);
    }
    if (!tree.issame(a[i], b[i]))
      ans++;
  }

  cout << ans << endl;

  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;
    else
      return par[x] = root(par[x]);
  }

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

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

int main() {
  int N, M;
  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]--;
  }

  int ans = 0;
  for (int i = 0; i < M; i++) {
    UnionFind tree(N);

    for (int j = 0; j < M; j++) {
      if (j == i)
        continue;
      tree.unite(a[j], b[j]);
    }
    if (!tree.issame(a[i], b[i]))
      ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int i = (m); (i) < (n); (i)++)
typedef long long ll;
vector<vector<int>> G(55);
int used[55] = {};

void dfs(int v) {
  used[v] = 1;
  for (int x : G[v]) {
    if (used[x] == 0) {
      dfs(x);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  int a[55], b[55];
  rep(i, m) { cin >> a[i] >> b[i]; }
  int res = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (j != i) {
        G[a[j]].push_back(b[j]);
        G[b[j]].push_back(a[j]);
      }
    }

    dfs(1);
    for (int j = 1; j <= n; j++) {
      if (used[j] == 0) {
        res++;
        break;
      }
    }
    G.clear();
    G.resize(50);
    fill(used, used + 50, 0);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int i = (m); (i) < (n); (i)++)
typedef long long ll;
vector<vector<int>> G(55);
int used[55] = {};

void dfs(int v) {
  used[v] = 1;
  for (int x : G[v]) {
    if (used[x] == 0) {
      dfs(x);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  int a[55], b[55];
  rep(i, m) { cin >> a[i] >> b[i]; }
  int res = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (j != i) {
        G[a[j]].push_back(b[j]);
        G[b[j]].push_back(a[j]);
      }
    }

    dfs(1);
    for (int j = 1; j <= n; j++) {
      if (used[j] == 0) {
        res++;
        break;
      }
    }
    G.clear();
    G.resize(55);
    fill(used, used + 55, 0);
  }
  cout << res << endl;
  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define INF 0x3f3f3f3f
typedef long long ll;

using namespace std;
struct A {
  int a;
  int b;
};
int vis[105];
A e[105];
int ans = 0;
vector<int> v[105];
queue<int> q;
int n, m;
int huan[105];

int main() {
  cin >> n >> m;
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
    vis[x]++;
    vis[y]++;
  }
  bool ok = true;
  while (ok) {
    ok = false;
    for (int i = 1; i <= n; i++)
      if (vis[i] == 1) {
        ok = true;
        for (int j = 0; j < v[i].size(); j++) {
          int k = v[i][j];
          vis[k]--;
        }
        ans++;
      }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define INF 0x3f3f3f3f
typedef long long ll;

using namespace std;
struct A {
  int a;
  int b;
};
int vis[105];
A e[105];
int ans = 0;
vector<int> v[105];
queue<int> q;
int n, m;
int huan[105];

int main() {
  cin >> n >> m;
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
    vis[x]++;
    vis[y]++;
  }
  bool ok = true;
  while (ok) {
    ok = false;
    for (int i = 1; i <= n; i++)
      if (vis[i] == 1) {
        ok = true;
        for (int j = 0; j < v[i].size(); j++) {
          int k = v[i][j];
          vis[k]--;
        }
        ans++;
        vis[i]--;
      }
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr << " " << (hoge) << endl;
#define DBG cerr << "!" << endl;
#define BITLE(n) (1LL << ((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s, f, t) ((s).substr((f)-1, (t) - (f) + 1))
#define ALL(a) (a).begin(), (a).end()
using namespace std;
ll cnt = 0;
bool vis[100];
vector<pair<ll, ll>> pll;
void dfs(ll now, ll unused) {
  cnt++;
  ll m = pll.size();
  for (int i = 0; i < m; i++) {
    if (i == unused)
      continue;
    if (pll[i].F == now) {
      if (vis[pll[i].S] == false) {
        vis[pll[i].S] = true;
        dfs(pll[i].S, unused);
        //	vis[pll[i].S]=false;
      }
    } else if (pll[i].S == now) {
      if (vis[pll[i].F] == false) {
        vis[pll[i].F] = true;
        dfs(pll[i].F, unused);
        // vis[pll[i].F]=false;
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  pll.resize(n);
  ll ans = 0;
  for (int i = 0; i < m; i++)
    cin >> pll[i].F >> pll[i].S;
  for (int i = 0; i < m; i++) {
    cnt = 0;
    for (int j = 1; j <= 50; j++)
      vis[j] = false;
    vis[1] = true;
    dfs(1, i);
    // cout<<cnt<<" "<<pll[i].F<<" "<<pll[i].S<<endl;
    if (cnt != n) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr << " " << (hoge) << endl;
#define DBG cerr << "!" << endl;
#define BITLE(n) (1LL << ((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s, f, t) ((s).substr((f)-1, (t) - (f) + 1))
#define ALL(a) (a).begin(), (a).end()
using namespace std;
ll cnt = 0;
bool vis[100];
vector<pair<ll, ll>> pll;
void dfs(ll now, ll unused) {
  cnt++;
  ll m = pll.size();
  for (int i = 0; i < m; i++) {
    if (i == unused)
      continue;
    if (pll[i].F == now) {
      if (vis[pll[i].S] == false) {
        vis[pll[i].S] = true;
        dfs(pll[i].S, unused);
        //	vis[pll[i].S]=false;
      }
    } else if (pll[i].S == now) {
      if (vis[pll[i].F] == false) {
        vis[pll[i].F] = true;
        dfs(pll[i].F, unused);
        // vis[pll[i].F]=false;
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  pll.resize(m);
  ll ans = 0;
  for (int i = 0; i < m; i++)
    cin >> pll[i].F >> pll[i].S;
  for (int i = 0; i < m; i++) {
    cnt = 0;
    for (int j = 1; j <= 50; j++)
      vis[j] = false;
    vis[1] = true;
    dfs(1, i);
    // cout<<cnt<<" "<<pll[i].F<<" "<<pll[i].S<<endl;
    if (cnt != n) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

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

using namespace std;

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {

  int N, M;
  cin >> N >> M;
  vector<int> a(M);
  vector<int> b(M);

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

  int res = 0;
  for (int i = 0; i < M; i++) {
    UnionFind bridges(N);
    for (int j = 0; j < M; j++) {
      if (j != i) {
        bridges.unite(a[j], b[j]);
      }
    }

    int rtn = bridges.root(0);
    for (int j = 1; j < N; j++) {
      if (rtn != bridges.root(j)) {
        res += 1;
        break;
      }
    }
  }

  cout << res << endl;

  return 0;
}

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

using namespace std;

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {

  int N, M;
  cin >> N >> M;
  vector<int> a(M);
  vector<int> b(M);

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

  int res = 0;
  for (int i = 0; i < M; i++) {
    UnionFind bridges(N);
    for (int j = 0; j < M; j++) {
      if (j != i) {
        bridges.unite(a[j] - 1, b[j] - 1);
      }
    }

    int rtn = bridges.root(0);
    for (int j = 1; j < N; j++) {
      if (rtn != bridges.root(j)) {
        res += 1;
        break;
      }
    }
  }

  cout << res << endl;

  return 0;
}
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;

struct unionFind {
  vector<int> d; // 根： 木のサイズ x -1, それ以外： 親のID
  unionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    // root化しておく
    x = find(x);
    y = find(y);
    if (x == y)
      return false;

    // x を y に unite
    // d[x] のほうが多きい（=サイズが小さい）場合はswap
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }

  int ans = 0;
  rep(i, m) {
    unionFind uf = unionFind(n);
    rep(j, m) if (i != j) uf.unite(x[j], y[j]);
    if (!uf.same(x[i], y[i]))
      ans++;
  }
  cout << ans << endl;
  return 0;
}

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

struct unionFind {
  vector<int> d; // 根： 木のサイズ x -1, それ以外： 親のID
  unionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    // root化しておく
    x = find(x);
    y = find(y);
    if (x == y)
      return false;

    // x を y に unite
    // d[x] のほうが多きい（=サイズが小さい）場合はswap
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m), y(m);
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }

  int ans = 0;
  rep(i, m) {
    unionFind uf = unionFind(n);
    rep(j, m) if (i != j) uf.unite(x[j], y[j]);
    if (!uf.same(x[i], y[i]))
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
// constexpr ll MOD = 998244353;
constexpr ll MOD = 1000000007;

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;
    else
      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(void) {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> E;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    E.emplace_back(a, b);
  }
  int cnt = 0;
  for (int i = 0; i < M; i++) {
    UnionFind V(N);
    pair<int, int> P;
    P = E.front();
    E.erase(E.begin());
    for (auto itr : E) {
      V.unite(itr.first, itr.second);
    }
    set<int> root;
    for (int j = 0; j < N; j++) {
#ifdef DEBUG
      cout << V.root(j) << endl;
#endif
      root.insert(V.root(j));
    }
    if (root.size() != 1)
      cnt++;
    E.push_back(P);
  }
  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
// constexpr ll MOD = 998244353;
constexpr ll MOD = 1000000007;

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;
    else
      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(void) {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> E;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    E.emplace_back(a - 1, b - 1);
  }
  int cnt = 0;
  for (int i = 0; i < M; i++) {
    UnionFind V(N);
    pair<int, int> P;
    P = E.front();
    E.erase(E.begin());
    for (auto itr : E) {
      V.unite(itr.first, itr.second);
    }
    set<int> root;
    for (int j = 0; j < N; j++) {
#ifdef DEBUG
      cout << V.root(j) << endl;
#endif
      root.insert(V.root(j));
    }
    if (root.size() != 1)
      cnt++;
    E.push_back(P);
  }
  cout << cnt << endl;

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <vector>
using ll = long long;
using namespace std;
int n;
int c = 0;
int esx;
int esy;
vector<vector<int>> vec;
vector<int> reg;
void dfs(int x) {
  //  cout<<"  "<<x<<endl;
  for (int i = 0; i < vec[x].size(); i++) {
    if (reg[vec[x][i]] == 0) {
      if ((x == esx && vec[x][i] == esy) || (x == esy && vec[x][i] == esx))
        continue;
      reg[vec[x][i]] = 1;
      dfs(vec[x][i]);
    }
  }
  return;
}
int main() {
  int m;
  cin >> n >> m;
  vec.resize(n + 1);
  reg.resize(n + 1);
  vector<pair<int, int>> es(n + 1);
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    vec[a].push_back(b);
    vec[b].push_back(a);
    es[i] = pair<int, int>(a, b);
  }
  for (int i = 1; i <= m; i++) {
    esx = es[i].first;
    esy = es[i].second;
    reg[1] = 1;
    dfs(1);
    bool h = true;
    for (int j = 1; j <= n; j++) {
      if (reg[j] == 0) {
        h = false;
        break;
      }
    }
    if (!h) {
      c++;
      //    cout<<i<<endl;
    }
    for (int j = 1; j <= n; j++) {
      reg[j] = 0;
    }
  }
  cout << c << endl;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <vector>
using ll = long long;
using namespace std;
int n;
int c = 0;
int esx;
int esy;
vector<vector<int>> vec;
vector<int> reg;
void dfs(int x) {
  //  cout<<"  "<<x<<endl;
  for (int i = 0; i < vec[x].size(); i++) {
    if (reg[vec[x][i]] == 0) {
      if ((x == esx && vec[x][i] == esy) || (x == esy && vec[x][i] == esx))
        continue;
      reg[vec[x][i]] = 1;
      dfs(vec[x][i]);
    }
  }
  return;
}
int main() {
  int m;
  cin >> n >> m;
  vec.resize(n + 100);
  reg.resize(n + 100);
  vector<pair<int, int>> es(n + 100);
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    vec[a].push_back(b);
    vec[b].push_back(a);
    es[i] = pair<int, int>(a, b);
  }
  for (int i = 1; i <= m; i++) {
    esx = es[i].first;
    esy = es[i].second;
    reg[1] = 1;
    dfs(1);
    bool h = true;
    for (int j = 1; j <= n; j++) {
      if (reg[j] == 0) {
        h = false;
        break;
      }
    }
    if (!h) {
      c++;
      //    cout<<i<<endl;
    }
    for (int j = 1; j <= n; j++) {
      reg[j] = 0;
    }
  }
  cout << c << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

int N, M;

int root(int x, vector<int> &par) {
  if (par[x] == x)
    return x;
  return par[x] = root(par[x], par);
}

void unite(int x, int y, vector<int> &par, vector<int> &group) {
  x = root(x, par);
  y = root(y, par);
  if (x == y)
    return;
  par[x] = y;
  group[y] += group[x];
}

int main() {
  cin >> N >> M;
  vector<pair<int, int>> edge(N + 1);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    edge[i] = make_pair(a - 1, b - 1);
  }
  int ans = M;
  for (int i = 0; i < M; i++) {
    vector<int> par(N), group(N, 1);
    for (int j = 0; j < N; j++)
      par[j] = j;
    for (int j = 0; j < M; j++) {
      if (i == j)
        continue;
      unite(edge[j].first, edge[j].second, par, group);
    }
    if (group[root(0, par)] == N)
      --ans;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

int N, M;

int root(int x, vector<int> &par) {
  if (par[x] == x)
    return x;
  return par[x] = root(par[x], par);
}

void unite(int x, int y, vector<int> &par, vector<int> &group) {
  x = root(x, par);
  y = root(y, par);
  if (x == y)
    return;
  par[x] = y;
  group[y] += group[x];
}

int main() {
  cin >> N >> M;
  vector<pair<int, int>> edge(M);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    edge[i] = make_pair(a - 1, b - 1);
  }
  int ans = M;
  for (int i = 0; i < M; i++) {
    vector<int> par(N), group(N, 1);
    for (int j = 0; j < N; j++)
      par[j] = j;
    for (int j = 0; j < M; j++) {
      if (i == j)
        continue;
      unite(edge[j].first, edge[j].second, par, group);
    }
    if (group[root(0, par)] == N)
      --ans;
  }
  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define PB push_back
#define ALL(_v) (_v).begin(), (_v).end()
#define SZ size()
typedef long long LL;

using namespace std;

struct xyz {
  int a;
  int b;
};

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

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

  int ans = 0;
  for (int i = 0; i < m; i++) {
    bool flag[n][n];

    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        flag[j][k] = false;
      }
    }

    for (int j = 0; j < n; j++) {
      vector<int> ad(1, j);
      flag[j][j] = true;

      while (ad.empty() == false) {
        int last = ad[ad.size() - 1];
        ad.pop_back();
        for (int k = 0; k < m; k++) {
          if (i != k) {
            if (v[k].a - 1 == last && flag[j][v[k].b - 1] == false) {
              flag[j][v[k].b - 1] = true;
              ad.PB(v[k].b - 1);
            }
            if (v[k].b - 1 == last && flag[j][v[k].a - 1] == false) {
              flag[j][v[k].a - 1] = true;
              ad.PB(v[k].a - 1);
            }
          }
        }
      }
    }

    bool check = false;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (flag[j][k] == false) {
          check = true;
          ans++;
          break;
        }
      }
      if (check == true) {
        break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define PB push_back
#define ALL(_v) (_v).begin(), (_v).end()
#define SZ size()
typedef long long LL;

using namespace std;

struct xyz {
  int a;
  int b;
};

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

  vector<xyz> v(m);
  for (int i = 0; i < m; i++) {
    cin >> v[i].a >> v[i].b;
  }

  int ans = 0;
  for (int i = 0; i < m; i++) {
    bool flag[n][n];

    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        flag[j][k] = false;
      }
    }

    for (int j = 0; j < n; j++) {
      vector<int> ad(1, j);
      flag[j][j] = true;

      while (ad.empty() == false) {
        int last = ad[ad.size() - 1];
        ad.pop_back();
        for (int k = 0; k < m; k++) {
          if (i != k) {
            if (v[k].a - 1 == last && flag[j][v[k].b - 1] == false) {
              flag[j][v[k].b - 1] = true;
              ad.PB(v[k].b - 1);
            }
            if (v[k].b - 1 == last && flag[j][v[k].a - 1] == false) {
              flag[j][v[k].a - 1] = true;
              ad.PB(v[k].a - 1);
            }
          }
        }
      }
    }

    bool check = false;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (flag[j][k] == false) {
          check = true;
          ans++;
          break;
        }
      }
      if (check == true) {
        break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

vector<vector<long long>> G(50, vector<long long>(50, 0));
long long n, m;

vector<bool> visit(50, false);

void dfs(long long v) {
  visit[v] = true;

  for (long long i = 0; i < n; i++) {
    if (visit[i] == true)
      continue;

    if (G[v][i] == 0)
      continue;

    dfs(i);
  }
}

long long counter() {
  long long cnt = 0;
  for (long long i = 0; i < n; i++) {
    if (visit[i] == true)
      cnt++;
  }
  return cnt;
}

int main() {

  cin >> n >> m;

  vector<long long> a(n);
  vector<long long> b(n);

  for (long long i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    G[a[i]][b[i]] = 1;
    G[b[i]][a[i]] = 1;
  }

  long long ans = 0;
  for (long long i = 0; i < m; i++) {
    G[a[i]][b[i]] = 0;
    G[b[i]][a[i]] = 0;

    dfs(0);
    if (counter() != n)
      ans++;

    G[a[i]][b[i]] = 1;
    G[b[i]][a[i]] = 1;
    visit.assign(50, false);
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

vector<vector<long long>> G(50, vector<long long>(50, 0));
long long n, m;

vector<bool> visit(50, false);

void dfs(long long v) {
  visit[v] = true;

  for (long long i = 0; i < n; i++) {
    if (visit[i] == true)
      continue;

    if (G[v][i] == 0)
      continue;

    dfs(i);
  }
}

long long counter() {
  long long cnt = 0;
  for (long long i = 0; i < n; i++) {
    if (visit[i] == true)
      cnt++;
  }
  return cnt;
}

int main() {

  cin >> n >> m;

  vector<long long> a(m);
  vector<long long> b(m);

  for (long long i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    G[a[i]][b[i]] = 1;
    G[b[i]][a[i]] = 1;
  }

  long long ans = 0;
  for (long long i = 0; i < m; i++) {
    G[a[i]][b[i]] = 0;
    G[b[i]][a[i]] = 0;

    dfs(0);
    if (counter() != n)
      ans++;

    G[a[i]][b[i]] = 1;
    G[b[i]][a[i]] = 1;
    visit.assign(50, false);
  }

  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

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

typedef pair<int, int> P;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define sz(x) ((int)(x).size())
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
vector<int> graph[55];

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (-d[x] < -d[y])
      swap(x, y); // y.size()が小でx.size()が大にしておきたい
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int ans = 0;
  rep(i, m) {
    UnionFind uf(m);
    for (int j = 0; j < m; j++) {
      if (i == j)
        continue;
      uf.unite(a[j], b[j]);
    }
    if (uf.size(0) != n)
      ans++;
  }
  cout << ans << endl;
}

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

typedef pair<int, int> P;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define sz(x) ((int)(x).size())
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
vector<int> graph[55];

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (-d[x] < -d[y])
      swap(x, y); // y.size()が小でx.size()が大にしておきたい
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int ans = 0;
  rep(i, m) {
    UnionFind uf(n);
    for (int j = 0; j < m; j++) {
      if (i == j)
        continue;
      uf.unite(a[j], b[j]);
    }
    if (uf.size(0) != n)
      ans++;
  }
  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// 2 4 8
// 2 4 6: 1 -> 2.5 > 4.25
// 6 4 2: 3 ->

using namespace std;

using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

bool dfs(int goal, int cur, int distance, const vector<vector<int>> &vs,
         vector<bool> &filled) {
  if (filled[cur]) {
    if (goal == cur) {
      // cout << "* cur:" << cur << " goal:" << goal << " dist:" << distance <<
      // endl;
      return distance >= 3;
    } else {
      return false;
    }
  }

  filled[cur] = true;
  for (int v : vs[cur]) {
    // cout << "goal:" << goal << "cur:" << cur << " v:" << v << endl;
    if (dfs(goal, v, distance + 1, vs, filled)) {
      return true;
    }
  }

  return false;
}

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

  vector<vector<int>> vs(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vs[a].push_back(b);
    vs[b].push_back(a);
  }
  vector<bool> on_loop(n, false);

  rep(i, n) {
    vector<bool> filled(n, false);

    if (dfs(i, i, 0, vs, filled)) {
      on_loop[i] = true;
    }
  }
  int count = 0;

  for (int x = 0; x < m; x++) {
    for (int y : vs[x]) {
      if (x >= y) {
        continue;
      }
      if (on_loop[x] == false || on_loop[y] == false) {
        count++;
      }
    }
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// 2 4 8
// 2 4 6: 1 -> 2.5 > 4.25
// 6 4 2: 3 ->

using namespace std;

using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

bool dfs(int goal, int cur, int distance, const vector<vector<int>> &vs,
         vector<bool> &filled) {
  if (filled[cur]) {
    if (goal == cur) {
      // cout << "* cur:" << cur << " goal:" << goal << " dist:" << distance <<
      // endl;
      return distance >= 3;
    } else {
      return false;
    }
  }

  filled[cur] = true;
  for (int v : vs[cur]) {
    // cout << "goal:" << goal << "cur:" << cur << " v:" << v << endl;
    if (dfs(goal, v, distance + 1, vs, filled)) {
      return true;
    }
  }

  return false;
}

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

  vector<vector<int>> vs(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vs[a].push_back(b);
    vs[b].push_back(a);
  }
  vector<bool> on_loop(n, false);

  rep(i, n) {
    vector<bool> filled(n, false);

    if (dfs(i, i, 0, vs, filled)) {
      on_loop[i] = true;
    }
  }
  int count = 0;

  for (int x = 0; x < n; x++) {
    for (int y : vs[x]) {
      if (x >= y) {
        continue;
      }
      if (on_loop[x] == false || on_loop[y] == false) {
        count++;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p03575 (C++)

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

using namespace std;

const int MAX = 50;

class UnionFind {
public:
  // O(N)
  UnionFind(int n) {
    for (int i = 0; i < n; i++) {
      data[i] = i;
    }
    num_subtrees = n;
  }

  // O(N)
  int root(int x) {
    int rx = data[x];
    while (rx != data[rx]) {
      rx = data[rx];
    }
    return rx;
  }

  // O(N)
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    data[ry] = rx;
    num_subtrees--;
    return;
  }

  // O(1)
  bool complete() { return num_subtrees == 1; }

private:
  int data[MAX];
  int num_subtrees;
};

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

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

  int ans = M;

  // O(N^3)
  // Choose which to omit
  for (int i = 0; i < M; i++) {
    UnionFind uf(N);
    for (int j = 0; j < M; j++) {
      if (i == j)
        continue;
      uf.unite(a[j], b[j]);
    }
    if (uf.complete()) {
      ans--;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <iostream>

using namespace std;

const int MAX = 50;

class UnionFind {
public:
  // O(N)
  UnionFind(int n) {
    for (int i = 0; i < n; i++) {
      data[i] = i;
    }
    num_subtrees = n;
  }

  // O(N)
  int root(int x) {
    int rx = data[x];
    while (rx != data[rx]) {
      rx = data[rx];
    }
    return rx;
  }

  // O(N)
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    data[ry] = rx;
    num_subtrees--;
    return;
  }

  // O(1)
  bool complete() { return num_subtrees == 1; }

private:
  int data[MAX];
  int num_subtrees;
};

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

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

  int ans = M;

  // O(N^3)
  // Choose which to omit
  for (int i = 0; i < M; i++) {
    UnionFind uf(N);
    for (int j = 0; j < M; j++) {
      if (i == j)
        continue;
      uf.unite(a[j], b[j]);
    }
    if (uf.complete()) {
      ans--;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int siz = 50;
vector<int> adj[siz];
vector<bool> visited(siz);
vector<int> tin(siz), low(siz);
int timer;
int ans;
int n, m;
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])
        ans++;
    }
  }
}

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() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  find_bridges();
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int siz = 100;
vector<int> adj[siz];
vector<bool> visited(siz);
vector<int> tin(siz), low(siz);
int timer;
int ans;
int n, m;
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])
        ans++;
    }
  }
}

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() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  find_bridges();
  cout << ans << endl;
}
Problem p03575 (C++)

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

using namespace std;

#define inf 1 << 30
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define fn fill_n
typedef long long llong;
typedef unsigned long long ullong;
typedef pair<int, int> P;
struct UnionFind {
  vector<int> par, rank;

  UnionFind() {}
  UnionFind(int n) {
    par.resize(n), rank.resize(n);
    for (int i = 0; i < n; i++)
      par[i] = i, rank[i] = 0;
  }
  int root(int n) {
    if (par[n] == n)
      return n;
    par[n] = root(par[n]);
  }
  bool same(int a, int b) { return root(a) == root(b); }
  bool marge(int a, int b) {
    a = root(a), b = root(b);
    if (a == b)
      return false;
    if (rank[a] < rank[b])
      swap(a, b);
    if (rank[a] == rank[b])
      rank[a]++;
    par[b] = a;
    return true;
  }
};

int main() {
  int n, m;
  P edge[50];
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> edge[i].first >> edge[i].second;

  int cnt = 0;
  for (int i = 0; i < m; i++) {
    UnionFind uf(n + 1);
    for (int j = 0; j < m; j++) {
      if (i == j)
        continue;
      uf.marge(edge[j].first, edge[j].second);
    }
    if (!uf.same(edge[i].first, edge[i].second))
      cnt++;
  }

  cout << cnt << endl;

  return 0;
}

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

using namespace std;

#define inf 1 << 30
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define fn fill_n
typedef long long llong;
typedef unsigned long long ullong;
typedef pair<int, int> P;
struct UnionFind {
  vector<int> par, rank;

  UnionFind() {}
  UnionFind(int n) {
    par.resize(n), rank.resize(n);
    for (int i = 0; i < n; i++)
      par[i] = i, rank[i] = 0;
  }
  int root(int n) {
    if (par[n] == n)
      return n;
    return par[n] = root(par[n]);
  }
  bool same(int a, int b) { return root(a) == root(b); }
  bool marge(int a, int b) {
    a = root(a), b = root(b);
    if (a == b)
      return false;
    if (rank[a] < rank[b])
      swap(a, b);
    if (rank[a] == rank[b])
      rank[a]++;
    par[b] = a;
    return true;
  }
};

int main() {
  int n, m;
  P edge[50];
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> edge[i].first >> edge[i].second;

  int cnt = 0;
  for (int i = 0; i < m; i++) {
    UnionFind uf(n + 1);
    for (int j = 0; j < m; j++) {
      if (i == j)
        continue;
      uf.marge(edge[j].first, edge[j].second);
    }
    if (!uf.same(edge[i].first, edge[i].second))
      cnt++;
  }

  cout << cnt << endl;

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pi;

int N, M;
pi del_pth;
vector<bool> vis;
vector<pi> P;
vector<vi> G;

void dfs(int s) {
  if (vis[s])
    return;
  vis[s] = true;
  for (int i = 0; i < (int)G[s].size(); ++i) {
    int t = G[s][i];
    pi pth = (s < t) ? make_pair(s, t) : make_pair(t, s);
    if (pth != del_pth)
      dfs(t);
  }
}

bool check(void) {
  bool f = false;
  for (int i = 1; i <= N; ++i) {
    if (vis[i] == false) {
      f = true;
      break;
    }
  }
  return f;
}

int main(void) {
  cin >> N >> M;
  P.resize(N);
  G.resize(N + 1);

  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    P[i] = make_pair(a, b);
    G[a].push_back(b);
    G[b].push_back(a);
  }

  int ans = 0;
  for (int i = 0; i < M; ++i) {
    del_pth = P[i];
    vis.clear();
    vis.resize(N + 1, false);
    dfs(1);
    bool res = check();
    if (res)
      ++ans;
  }

  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pi;

int N, M;
pi del_pth;
vector<bool> vis;
vector<pi> P;
vector<vi> G;

void dfs(int s) {
  if (vis[s])
    return;
  vis[s] = true;
  for (int i = 0; i < (int)G[s].size(); ++i) {
    int t = G[s][i];
    pi pth = (s < t) ? make_pair(s, t) : make_pair(t, s);
    if (pth != del_pth)
      dfs(t);
  }
}

bool check(void) {
  bool f = false;
  for (int i = 1; i <= N; ++i) {
    if (vis[i] == false) {
      f = true;
      break;
    }
  }
  return f;
}

int main(void) {
  cin >> N >> M;
  P.resize(M);
  G.resize(N + 1);

  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    P[i] = make_pair(a, b);
    G[a].push_back(b);
    G[b].push_back(a);
  }

  int ans = 0;
  for (int i = 0; i < M; ++i) {
    del_pth = P[i];
    vis.clear();
    vis.resize(N + 1, false);
    dfs(1);
    bool res = check();
    if (res)
      ++ans;
  }

  cout << ans << '\n';
  return 0;
}
Problem p03575 (C++)

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

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)

using namespace std;

typedef long long int lli;

class UF {
public:
  vector<int> par;
  int cnt;
  UF(int n) : par(n), cnt(n) { REP(i, 0, n) par[i] = i; }
  int Find(int a) { return (par[a] == a ? a : par[a] = Find(par[a])); }
  void Union(int a, int b) {
    a = Find(a);
    b = Find(b);
    if (a == b)
      return;
    par[a] = b;
    cnt--;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(N), b(N);
  REP(i, 0, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int ans = 0;
  REP(i, 0, M) {
    UF uf(N);
    REP(j, 0, M) {
      if (i == j)
        continue;
      uf.Union(a[j], b[j]);
    }
    ans += (uf.cnt != 1);
  }
  cout << ans << endl;
  return 0;
}

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

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)

using namespace std;

typedef long long int lli;

class UF {
public:
  vector<int> par;
  int cnt;
  UF(int n) : par(n), cnt(n) { REP(i, 0, n) par[i] = i; }
  int Find(int a) { return (par[a] == a ? a : par[a] = Find(par[a])); }
  void Union(int a, int b) {
    a = Find(a);
    b = Find(b);
    if (a == b)
      return;
    par[a] = b;
    cnt--;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M), b(M);
  REP(i, 0, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int ans = 0;
  REP(i, 0, M) {
    UF uf(N);
    REP(j, 0, M) {
      if (i == j)
        continue;
      uf.Union(a[j], b[j]);
    }
    ans += (uf.cnt != 1);
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

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

#define ll long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
bool used[55];
int cnt;
int n;
bool ok;
void dfs(vector<vector<int>> graph, int x) {
  cnt++;
  used[x] = true;
  if (cnt == n) {
    ok = true;
    return;
  }
  rep(i, 0, graph[x].size()) {
    if (!used[graph[x][i]])
      dfs(graph, graph[x][i]);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int m;
  cin >> n >> m;
  vector<vector<int>> graph(n);
  vector<pair<int, int>> memo(n);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
    memo[i] = make_pair(a, b);
  }
  rep(i, 0, n) sort(all(graph[i]));
  int ans = 0;
  rep(i, 0, m) {
    graph[memo[i].first].erase(
        lower_bound(all(graph[memo[i].first]), memo[i].second));
    graph[memo[i].second].erase(
        lower_bound(all(graph[memo[i].second]), memo[i].first));
    rep(i, 0, 55) used[i] = false;
    cnt = 0;
    ok = false;
    dfs(graph, 0);
    if (!ok)
      ans++;
    graph[memo[i].first].push_back(memo[i].second);
    graph[memo[i].second].push_back(memo[i].first);
    sort(all(graph[memo[i].first]));
    sort(all(graph[memo[i].second]));
  }
  cout << ans << endl;
}

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

#define ll long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
bool used[55];
int cnt;
int n;
bool ok;
void dfs(vector<vector<int>> graph, int x) {
  cnt++;
  used[x] = true;
  if (cnt == n) {
    ok = true;
    return;
  }
  rep(i, 0, graph[x].size()) {
    if (!used[graph[x][i]])
      dfs(graph, graph[x][i]);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int m;
  cin >> n >> m;
  vector<vector<int>> graph(n);
  vector<pair<int, int>> memo(m);
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
    memo[i] = make_pair(a, b);
  }
  rep(i, 0, n) sort(all(graph[i]));
  int ans = 0;
  rep(i, 0, m) {
    graph[memo[i].first].erase(
        lower_bound(all(graph[memo[i].first]), memo[i].second));
    graph[memo[i].second].erase(
        lower_bound(all(graph[memo[i].second]), memo[i].first));
    rep(i, 0, 55) used[i] = false;
    cnt = 0;
    ok = false;
    dfs(graph, 0);
    if (!ok)
      ans++;
    graph[memo[i].first].push_back(memo[i].second);
    graph[memo[i].second].push_back(memo[i].first);
    sort(all(graph[memo[i].first]));
    sort(all(graph[memo[i].second]));
  }
  cout << ans << endl;
}
Problem p03575 (C++)

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

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 n, m;
  cin >> n >> m;
  vector<int> data[n];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    data[a].push_back(b);
    data[b].push_back(a);
  }
  bool update = true;
  int ans = 0;
  while (update) {
    update = false;
    rep(i, n) {
      if (data[i].size() == 0)
        continue;
      else if (data[i].size() == 1) {
        int index = data[i].back();
        data[i].pop_back();
        data[index].erase(data[index].begin());
        ++ans;
        update = true;
      }
    }
  }
  cout << ans << endl;
}

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

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 n, m;
  cin >> n >> m;
  vector<int> data[n];
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    data[a].push_back(b);
    data[b].push_back(a);
  }
  bool update = true;
  int ans = 0;
  while (update) {
    update = false;
    rep(i, n) {
      if (data[i].size() == 0)
        continue;
      else if (data[i].size() == 1) {
        int index = data[i].back();
        data[i].pop_back();
        auto e = remove(data[index].begin(), data[index].end(), i);
        data[index].erase(e, data[index].end());
        //  cout << "---" << i << " " << index << "---" << endl;
        // data[index].erase(data[index].begin());
        ++ans;
        update = true;
      }
    }
  }
  cout << ans << endl;
}