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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct UnionFind {
  vector<int> par, siz;
  UnionFind(int n) : par(n), siz(n, 1) { rep(i, n) par[i] = i; }
  int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return siz[find(x)]; }
};

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

  int ans = 0;
  rep(i, m) {
    UnionFind uf(n);
    rep(j, m) {
      if (i == j)
        continue;
      uf.unite(a[j], b[j]);
    }
    if (uf.size(a[i]) != n || uf.size(b[i]) != n)
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct UnionFind {
  vector<int> par, siz;
  UnionFind(int n) : par(n), siz(n, 1) { rep(i, n) par[i] = i; }
  int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return siz[find(x)]; }
};

int main() {
  int n, m;
  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) {
    UnionFind uf(n);
    rep(j, m) {
      if (i == j)
        continue;
      uf.unite(a[j], b[j]);
    }
    if (uf.size(a[i]) != n || uf.size(b[i]) != n)
      ans++;
  }
  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAX 1000000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
const ll INF = 10010010010001;
vector<vector<bool>> graph;
vector<bool> reached;
vector<int> A, B;
int N, M;

void dfs(int v) {
  reached[v] = true;
  for (int i = 0; i < N; i++) {
    if (graph[v][i] == false)
      continue;
    if (reached[i])
      continue;
    dfs(i);
  }
}

int main() {
  cin >> N >> M;
  graph = vector<vector<bool>>(N, vector<bool>(N, false));
  reached = vector<bool>(N);
  A = vector<int>(N);
  B = vector<int>(N);
  for (int i = 0; i < M; i++) {
    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;
    for (int j = 0; j < N; j++)
      reached[j] = false;
    dfs(0);
    bool bridge = false;
    for (int j = 0; j < N; j++) {
      if (reached[j] == false) {
        bridge = true;
        break;
      }
    }
    if (bridge)
      ans++;
    graph[A[i]][B[i]] = graph[B[i]][A[i]] = true;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAX 1000000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
const ll INF = 10010010010001;
vector<vector<bool>> graph;
vector<bool> reached;
vector<int> A, B;
int N, M;

void dfs(int v) {
  reached[v] = true;
  for (int i = 0; i < N; i++) {
    if (graph[v][i] == false)
      continue;
    if (reached[i])
      continue;
    dfs(i);
  }
}

int main() {
  cin >> N >> M;
  graph = vector<vector<bool>>(50, vector<bool>(50, false));
  reached = vector<bool>(50);
  A = vector<int>(50);
  B = vector<int>(50);
  for (int i = 0; i < M; i++) {
    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;
    for (int j = 0; j < N; j++)
      reached[j] = false;
    dfs(0);
    bool bridge = false;
    for (int j = 0; j < N; j++) {
      if (reached[j] == false) {
        bridge = true;
        break;
      }
    }
    if (bridge)
      ans++;
    graph[A[i]][B[i]] = graph[B[i]][A[i]] = true;
  }
  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int n, m;
bool used[52];
bool flag[52];
int a[52], b[52];
int ans;
void dfs(int t) {
  flag[t] = true;
  for (int i = 0; i < m; ++i) {
    if (used[i] || (a[i] != t && b[i] != t))
      continue;
    used[i] = true;
    dfs(t == a[i] ? b[i] : a[i]);
    used[i] = false;
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
  }
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < m; ++j)
      used[j] = false;
    for (int j = 0; j < n; ++j)
      flag[j] = false;
    used[i] = true;
    dfs(0);
    bool c = true;
    for (int j = 0; j < n; ++j)
      c &= flag[j];
    if (!c)
      ++ans;
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int n, m;
bool used[52];
bool flag[52];
int a[52], b[52];
int ans;
void dfs(int t) {
  flag[t] = true;
  for (int i = 0; i < m; ++i) {
    if (used[i] || (a[i] != t && b[i] != t))
      continue;
    used[i] = true;
    dfs(t == a[i] ? b[i] : a[i]);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
  }
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < m; ++j)
      used[j] = false;
    for (int j = 0; j < n; ++j)
      flag[j] = false;
    used[i] = true;
    dfs(0);
    bool c = true;
    for (int j = 0; j < n; ++j)
      c &= flag[j];
    if (!c)
      ++ans;
  }
  cout << ans << endl;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1);
const ll MOD = 1000000007;

int N, M;
using Graph = vector<vector<int>>;

void bfs(Graph &G, vector<bool> &check, int x) {
  check[x] = true;
  for (int i = 1; i <= N; i++) {
    if (G[x][i] == 1) {
      if (check[i])
        continue;
      bfs(G, check, i);
    }
  }
  return;
}

int main() {
  cin >> N >> M;
  Graph G(N + 1, vector<int>(N + 1));

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

  int count = 0;
  for (int i = 0; i < M; i++) {
    G[a[i]][b[i]] = 0;
    G[b[i]][a[i]] = 0;
    vector<bool> check(N + 1, false);
    check[0] = true;

    bfs(G, check, 1);
    bool can = false;
    for (int i = 1; i <= N; i++) {
      if (!check[i])
        can = true;
    }
    if (can)
      count++;
    G[a[i]][b[i]] = 1;
    G[b[i]][a[i]] = 1;
  }

  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1);
const ll MOD = 1000000007;

int N, M;
using Graph = vector<vector<int>>;

void bfs(Graph &G, vector<bool> &check, int x) {
  check[x] = true;
  for (int i = 1; i <= N; i++) {
    if (G[x][i] == 1) {
      if (check[i])
        continue;
      bfs(G, check, i);
    }
  }
  return;
}

int main() {
  cin >> N >> M;
  Graph G(N + 1, vector<int>(N + 1));

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

  int count = 0;
  for (int i = 0; i < M; i++) {
    G[a[i]][b[i]] = 0;
    G[b[i]][a[i]] = 0;
    vector<bool> check(N + 1, false);
    check[0] = true;

    bfs(G, check, 1);
    bool can = false;
    for (int i = 1; i <= N; i++) {
      if (!check[i])
        can = true;
    }
    if (can)
      count++;
    G[a[i]][b[i]] = 1;
    G[b[i]][a[i]] = 1;
  }

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

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

typedef long long ll;
#define modulo 1000000007 // 10^9 + 7
#define MAX 1000000000    // 10^9

static const int limit = 50;
bool G[limit + 1][limit + 1];
bool visited[limit + 1];

int N, M;

bool dfs(int start) {
  for (int i = 1; i <= N; i++)
    visited[i] = false; // 初期化
  visited[start] = true;
  stack<int> S;
  S.push(start);

  while (!S.empty()) {
    int x = S.top();
    S.pop();
    for (int i = 1; i <= N; i++) {
      if (G[x][i] == true && visited[i] != true) {
        S.push(i);
        visited[i] = true;
      }
    }
  }

  int ans = 1;
  for (int i = 1; i <= N; i++)
    ans *= visited[i];
  if (ans)
    return true;
  else
    return false;
}

int main() {
  cin >> N >> M;
  int a[N + 1], b[N + 1];

  /* いらない boolはfalseで初期化される
  for (int i = 1; i <= N; i++) {
          for (int j = 1; j <= N; j++) {
                  G[i][j] = false;
          }
  }
  */

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

  int ans = 0;
  for (int i = 1; i <= M; i++) {
    G[a[i]][b[i]] = false;
    G[b[i]][a[i]] = false;
    if (!dfs(1)) {
      // cout << "(" << a[i] << " , " << b[i] << ")" << " ";
      ans++;
    }

    G[a[i]][b[i]] = true;
    G[b[i]][a[i]] = true;
  }
  cout << ans << endl;

  return 0;
}

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

typedef long long ll;
#define modulo 1000000007 // 10^9 + 7
#define MAX 1000000000    // 10^9

static const int limit = 50;
bool G[limit + 1][limit + 1];
bool visited[limit + 1];

int N, M;

bool dfs(int start) {
  for (int i = 1; i <= N; i++)
    visited[i] = false; // 初期化
  visited[start] = true;
  stack<int> S;
  S.push(start);

  while (!S.empty()) {
    int x = S.top();
    S.pop();
    for (int i = 1; i <= N; i++) {
      if (G[x][i] == true && visited[i] != true) {
        S.push(i);
        visited[i] = true;
      }
    }
  }

  int ans = 1;
  for (int i = 1; i <= N; i++)
    ans *= visited[i];
  if (ans)
    return true;
  else
    return false;
}

int main() {
  cin >> N >> M;
  int a[M + 1], b[M + 1];

  /* いらない boolはfalseで初期化される
  for (int i = 1; i <= N; i++) {
          for (int j = 1; j <= N; j++) {
                  G[i][j] = false;
          }
  }
  */

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

  int ans = 0;
  for (int i = 1; i <= M; i++) {
    G[a[i]][b[i]] = false;
    G[b[i]][a[i]] = false;
    if (!dfs(1)) {
      // cout << "(" << a[i] << " , " << b[i] << ")" << " ";
      ans++;
    }

    G[a[i]][b[i]] = true;
    G[b[i]][a[i]] = true;
  }
  cout << ans << endl;

  return 0;
}
Problem p03575 (C++)

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()

using namespace std;
using ll = long long;
using P = pair<int, int>;

class UnionFind {
public:
  vector<int> p;    // 親
  vector<int> rank; // サイズ・各集合の根のみ有効
  UnionFind(int n) {
    p.resize(n, -1);
    rank.resize(n, 1);
  }
  int root(int x) {
    if (p[x] == -1)
      return x;
    else
      return p[x] = root(p[x]); // 深さを 1 にしている
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] > rank[y])
      swap(x, y); // rankの小さいものを下につける
    rank[y] += rank[x];
    p[x] = y;
  }
  // xが属すグループのサイズ
  int size(int x) { return rank[root(x)]; }
  bool same(int x, int y) { return (root(x) == root(y)); }
};

void solve() {
  ll n, m, a, b;
  cin >> n >> m;
  vector<P> edge(n);
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    a--;
    b--;
    edge[i] = P(a, b);
  }

  int ans = 0;
  for (int removed = 0; removed < m; ++removed) {
    UnionFind uf(n);
    for (int i = 0; i < m; ++i) {
      if (i == removed)
        continue;
      uf.unite(edge[i].first, edge[i].second);
    }
    if (uf.size(0) < n)
      ans++;
  }
  cout << ans;
}

int main() {
  fastio;
  solve();

  return 0;
}

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()

using namespace std;
using ll = long long;
using P = pair<int, int>;

class UnionFind {
public:
  vector<int> p;    // 親
  vector<int> rank; // サイズ・各集合の根のみ有効
  UnionFind(int n) {
    p.resize(n, -1);
    rank.resize(n, 1);
  }
  int root(int x) {
    if (p[x] == -1)
      return x;
    else
      return p[x] = root(p[x]); // 深さを 1 にしている
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] > rank[y])
      swap(x, y); // rankの小さいものを下につける
    rank[y] += rank[x];
    p[x] = y;
  }
  // xが属すグループのサイズ
  int size(int x) { return rank[root(x)]; }
  bool same(int x, int y) { return (root(x) == root(y)); }
};

void solve() {
  ll n, m, a, b;
  cin >> n >> m;
  vector<P> edge(m);
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    a--;
    b--;
    edge[i] = P(a, b);
  }

  int ans = 0;
  for (int removed = 0; removed < m; ++removed) {
    UnionFind uf(n);
    for (int i = 0; i < m; ++i) {
      if (i == removed)
        continue;
      uf.unite(edge[i].first, edge[i].second);
    }
    if (uf.size(0) < n)
      ans++;
  }
  cout << ans;
}

int main() {
  fastio;
  solve();

  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)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001001;
const int mod = 1000000007;

vector<int> g[100];
vector<bool> vi(100);
map<P, bool> mp;

void dfs(int a) {
  vi[a] = true;
  for (auto &c : g[a]) {
    if (vi[c])
      continue;
    if (mp[P(a, c)])
      continue;
    dfs(c);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<P> p(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    p[i] = make_pair(a, b);
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  int ans = 0;
  rep(i, m) {
    mp[P(p[i].first, p[i].second)] = true;
    mp[P(p[i].second, p[i].first)] = true;
    rep(i, n) vi[i] = false;
    dfs(0);
    bool elf = true;
    rep(i, n) if (!vi[i]) elf = false;
    if (!elf)
      ans++;
    mp[P(p[i].first, p[i].second)] = false;
    mp[P(p[i].second, p[i].first)] = false;
  }
  cout << ans << 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 = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001001;
const int mod = 1000000007;

vector<int> g[100];
vector<bool> vi(100);
map<P, bool> mp;

void dfs(int a) {
  vi[a] = true;
  for (auto &c : g[a]) {
    if (vi[c])
      continue;
    if (mp[P(a, c)])
      continue;
    dfs(c);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<P> p(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    p[i] = make_pair(a, b);
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  int ans = 0;
  rep(i, m) {
    mp[P(p[i].first, p[i].second)] = true;
    mp[P(p[i].second, p[i].first)] = true;
    rep(i, n) vi[i] = false;
    dfs(0);
    bool elf = true;
    rep(i, n) if (!vi[i]) elf = false;
    if (!elf)
      ans++;
    mp[P(p[i].first, p[i].second)] = false;
    mp[P(p[i].second, p[i].first)] = false;
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (C++)

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

class DisjointSet {
public:
  vector<int> rank, p, s;

  DisjointSet(int size) {
    rank.resize(size);
    p.resize(size);
    s.resize(size);
    for (size_t i = 0; i < size; i++) {
      makeSet(i);
    }
  }
  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
    s[x] = 1;
  }
  bool same(int x, int y) { return findSet(x) == findSet(y); }
  void unite(int x, int y) { link(findSet(x), findSet(y)); }
  void link(int x, int y) {
    if (x == y)
      return;
    if (rank[x] > rank[y]) {
      p[y] = x;
      s[x] += s[y];
    } else {
      p[x] = y;
      s[y] += s[x];
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }
  int size(int x) { return s[findSet(x)]; }
  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  for (size_t i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }
  int ret = 0;
  for (size_t i = 0; i < M; i++) {
    DisjointSet set(N);
    for (size_t j = 0; j < M; j++) {
      if (j != i) {
        set.unite(A[j], B[j]);
      }
    }
    if (set.size(0) != N)
      ret++;
  }
  cout << ret << endl;
  return 0;
}

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

class DisjointSet {
public:
  vector<int> rank, p, s;

  DisjointSet(int size) {
    rank.resize(size);
    p.resize(size);
    s.resize(size);
    for (size_t i = 0; i < size; i++) {
      makeSet(i);
    }
  }
  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
    s[x] = 1;
  }
  bool same(int x, int y) { return findSet(x) == findSet(y); }
  void unite(int x, int y) { link(findSet(x), findSet(y)); }
  void link(int x, int y) {
    if (x == y)
      return;
    if (rank[x] > rank[y]) {
      p[y] = x;
      s[x] += s[y];
    } else {
      p[x] = y;
      s[y] += s[x];
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }
  int size(int x) { return s[findSet(x)]; }
  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  for (size_t i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }
  int ret = 0;
  for (size_t i = 0; i < M; i++) {
    DisjointSet set(N);
    for (size_t j = 0; j < M; j++) {
      if (j != i) {
        set.unite(A[j] - 1, B[j] - 1);
      }
    }
    if (set.size(0) != N)
      ret++;
  }
  cout << ret << endl;
  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m;

int A[55], B[55];
bool graph[55][55];

bool visited[55];

void dfs(int v) {
  visited[v] = true;
  for (int i = 0; i < n; i++) {
    if (graph[v][i] == false)
      continue;
    if (visited[i] == true)
      continue;
    dfs(i);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    A[i] = a;
    B[i] = b;
    graph[a - 1][b - 1] = true;
    graph[b - 1][a - 1] = true;
  }
  int counter = 0;
  for (int i = 0; i < m; i++) {
    graph[A[i] - 1][B[i] - 1] = false;
    graph[B[i] - 1][A[i] - 1] = false;
    for (int j = 0; j < n; j++)
      visited[j] = false;
    dfs(0);
    bool t = false;
    for (int j = 0; j < n; i++) {
      if (visited[i] == false) {
        t = true;
      }
    }
    graph[A[i] - 1][B[i] - 1] = true;
    graph[B[i] - 1][A[i] - 1] = true;
    if (t)
      counter++;
  }
  cout << counter << endl;
}


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

int A[55], B[55];
bool graph[55][55];

bool visited[55];

void dfs(int v) {
  visited[v] = true;
  for (int i = 0; i < n; i++) {
    if (graph[v][i] == false)
      continue;
    if (visited[i] == true)
      continue;
    dfs(i);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    A[i] = a;
    B[i] = b;
    graph[a - 1][b - 1] = true;
    graph[b - 1][a - 1] = true;
  }
  int counter = 0;
  for (int i = 0; i < m; i++) {
    graph[A[i] - 1][B[i] - 1] = false;
    graph[B[i] - 1][A[i] - 1] = false;
    for (int j = 0; j < n; j++)
      visited[j] = false;
    dfs(0);
    bool t = false;
    for (int j = 0; j < n; j++) {
      if (visited[j] == false) {
        t = true;
      }
    }
    graph[A[i] - 1][B[i] - 1] = true;
    graph[B[i] - 1][A[i] - 1] = true;
    if (t)
      counter++;
  }
  cout << counter << endl;
}
Problem p03575 (C++)

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

struct unionFind {
  int n;
  vector<int> v, s;

  //     void init(){
  //         // v.resize(n);
  //         // for(int i=0;i<n;i++)v[i]=i;
  //         v.assign(n,-1);
  //         s.assign(n,1);
  //     }

  unionFind(){};

  unionFind(int _n) {
    n = _n;

    v.resize(n, -1);
    s.resize(n, 1);

    // for (int i = 0; i < n; i++) {
    //   v.push_back(i);
    //   s.push_back(1);
    // }
  }

  int root(int i) {
    if (v[i] == -1)
      return i;
    else
      return v[i] = root(v[i]);
  }

  void join(int i, int j) {
    int ri = root(i);
    int rj = root(j);
    if (ri == rj)
      return;
    if (s[ri] < s[rj])
      swap(ri, rj);
    v[rj] = ri;
    s[ri] += s[rj];
  }

  int size(int i) { return s[root(i)]; }

  bool connected() { return size(0) == n; }
};

int main() {
  int n, m;
  cin >> n >> m;
  int ans = 0;
  vector<int> a(n), b(n);

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

  for (int i = 0; i < m; i++) {
    auto u = unionFind(n);
    for (int j = 0; j < m; j++) {
      if (j != i)
        u.join(a[j], b[j]);
    }

    // for(int i=0;i<n;i++)cout<<u.root(i)<<" ";
    // cout<<endl;
    // for(int i=0;i<n;i++)cout<<u.size(i)<<" ";
    // cout<<endl;

    // if (!u.connected()) ans++;
    if (u.size(0) != n)
      ans++;
  }
  //      cout<<endl;
  cout << ans << endl;
}

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

struct unionFind {
  int n;
  vector<int> v, s;

  //     void init(){
  //         // v.resize(n);
  //         // for(int i=0;i<n;i++)v[i]=i;
  //         v.assign(n,-1);
  //         s.assign(n,1);
  //     }

  unionFind(){};

  unionFind(int _n) {
    n = _n;

    v.resize(n, -1);
    s.resize(n, 1);

    // for (int i = 0; i < n; i++) {
    //   v.push_back(i);
    //   s.push_back(1);
    // }
  }

  int root(int i) {
    if (v[i] == -1)
      return i;
    else
      return v[i] = root(v[i]);
  }

  void join(int i, int j) {
    int ri = root(i);
    int rj = root(j);
    if (ri == rj)
      return;
    if (s[ri] < s[rj])
      swap(ri, rj);
    v[rj] = ri;
    s[ri] += s[rj];
  }

  int size(int i) { return s[root(i)]; }

  bool connected() { return size(0) == n; }
};

int main() {
  int n, m;
  cin >> n >> m;
  int ans = 0;
  vector<int> a(m), b(m);

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

  for (int i = 0; i < m; i++) {
    auto u = unionFind(n);
    for (int j = 0; j < m; j++) {
      if (j != i)
        u.join(a[j], b[j]);
    }

    // for(int i=0;i<n;i++)cout<<u.root(i)<<" ";
    // cout<<endl;
    // for(int i=0;i<n;i++)cout<<u.size(i)<<" ";
    // cout<<endl;

    // if (!u.connected()) ans++;
    if (u.size(0) != n)
      ans++;
  }
  //      cout<<endl;
  cout << ans << endl;
}
Problem p03575 (C++)

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

using namespace std;

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;

struct UF {
  vector<int> p;
  UF() {}
  UF(int n) : p(n, -1) {}
  int find(int a) { return p[a] < 0 ? a : p[a] = find(p[a]); }
  void unite(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b)
      return;
    if (-p[a] < -p[b])
      swap(a, b);
    p[a] += p[b];
    p[b] = a;
  }
  bool root(int a) { return p[a] < 0; }
  bool same(int a, int b) { return find(a) == find(b); }
  int size(int a) { return -p[find(a)]; }
};

int main() {

  // cin.tie(0); ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  VI a(n), b(n);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int ans = 0;
  rep(i, m) {
    UF ufz(n);
    rep(j, m) if (i != j) ufz.unite(a[j], b[j]);
    if (ufz.size(0) != n)
      ans++;
  }
  cout << ans << "\n";
  return 0;
}


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

using namespace std;

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;

struct UF {
  vector<int> p;
  UF() {}
  UF(int n) : p(n, -1) {}
  int find(int a) { return p[a] < 0 ? a : p[a] = find(p[a]); }
  void unite(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b)
      return;
    if (-p[a] < -p[b])
      swap(a, b);
    p[a] += p[b];
    p[b] = a;
  }
  bool root(int a) { return p[a] < 0; }
  bool same(int a, int b) { return find(a) == find(b); }
  int size(int a) { return -p[find(a)]; }
};

int main() {

  // cin.tie(0); ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  VI a(m), b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int ans = 0;
  rep(i, m) {
    UF ufz(n);
    rep(j, m) if (i != j) ufz.unite(a[j], b[j]);
    if (ufz.size(0) != n)
      ans++;
  }
  cout << ans << "\n";
  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)

static const double PI = 3.14159265358979;
static const ll MOD = 1e9 + 7;
static const ll INF = (ll)1 << 62;

static const int bn = 32;

ll n, m;
ll c[9][9] = {};
bool v[9] = {};

ll ma[51] = {};
ll mb[51] = {};

ll total = 0;
bool allvisited() {
  repe(i, 1, n) {
    if (!v[i]) {
      return false;
    }
  }
  return true;
}
bool allV = false;
void dfs(int k) {
  repe(i, 1, n) {
    if (k != i && c[k][i] && !v[i]) {
      v[i] = true;
      dfs(i);
      // v[i] = false;
    }
  }
}

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

  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    ma[i] = a;
    mb[i] = b;
    c[a][b] = 1;
    c[b][a] = 1;
  }

  rep(i, 0, m) {
    // cout << "!" << ma[i] << " " << mb[i] << endl;
    repe(i, 1, n) v[i] = false;
    c[ma[i]][mb[i]] = 0;
    c[mb[i]][ma[i]] = 0;
    allV = false;
    v[1] = true;
    dfs(1);
    if (!allvisited()) {
      total++;
    }
    c[ma[i]][mb[i]] = 1;
    c[mb[i]][ma[i]] = 1;
  }

  cout << total << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)

static const double PI = 3.14159265358979;
static const ll MOD = 1e9 + 7;
static const ll INF = (ll)1 << 62;

static const int bn = 32;

ll n, m;
ll c[51][51] = {};
bool v[51] = {};

ll ma[51] = {};
ll mb[51] = {};

ll total = 0;
bool allvisited() {
  repe(i, 1, n) {
    if (!v[i]) {
      return false;
    }
  }
  return true;
}
bool allV = false;
void dfs(int k) {
  repe(i, 1, n) {
    if (k != i && c[k][i] && !v[i]) {
      v[i] = true;
      dfs(i);
      // v[i] = false;
    }
  }
}

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

  rep(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    ma[i] = a;
    mb[i] = b;
    c[a][b] = 1;
    c[b][a] = 1;
  }

  rep(i, 0, m) {
    // cout << "!" << ma[i] << " " << mb[i] << endl;
    repe(i, 1, n) v[i] = false;
    c[ma[i]][mb[i]] = 0;
    c[mb[i]][ma[i]] = 0;
    allV = false;
    v[1] = true;
    dfs(1);
    if (!allvisited()) {
      total++;
    }
    c[ma[i]][mb[i]] = 1;
    c[mb[i]][ma[i]] = 1;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define sortv(x) sort(x.begin(), x.end())
#define dump(x) cerr << #x << " = " << x << endl
typedef long long ll;

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;
  }
} typedef UF;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N), B(N);
  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++) {
    UF uf(N);
    for (int j = 0; j < M; j++) {
      if (i != j)
        uf.unite(A[j], B[j]);
    }
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      if (uf.par[j] == j)
        cnt++;
    }
    if (cnt > 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define sortv(x) sort(x.begin(), x.end())
#define dump(x) cerr << #x << " = " << x << endl
typedef long long ll;

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;
  }
} typedef UF;

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++) {
    UF uf(N);
    for (int j = 0; j < M; j++) {
      if (i != j)
        uf.unite(A[j], B[j]);
    }
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      if (uf.par[j] == j)
        cnt++;
    }
    if (cnt > 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03575 (Python)

Buggy submission (status: Runtime Error):
from sys import stdin


def dfs(g, v, visited):
    visited[v] = True

    for v2 in range(len(visited)):
        if not g[v][v2]:
            continue
        if visited[v2]:
            continue
        dfs(g, v2, visited)


def main():
    n, m = map(int, stdin.readline().split())
    ab = [list(map(int, stdin.readline().split())) for _ in range(m)]
    g = [[False] * n for _ in range(n)]
    for a, b in ab:
        g[a - 1][b - 1] = True
        g[b - 1][a - 1] = True

    ans = 0

    for i in range(m):
        visited = [False for _ in range(n)]
        a, b = ab[i]
        g[a - 1][b - 1] = False
        g[b - 1][a - 1] = False

        dfs(g, i, visited)

        check = False
        for v in visited:
            if not v:
                check = True
        if check:
            ans += 1

        g[a - 1][b - 1] = True
        g[b - 1][a - 1] = True

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from sys import stdin


def dfs(g, v, visited):
    visited[v] = True

    for v2 in range(len(visited)):
        if not g[v][v2]:
            continue
        if visited[v2]:
            continue
        dfs(g, v2, visited)


def main():
    n, m = map(int, stdin.readline().split())
    ab = [list(map(int, stdin.readline().split())) for _ in range(m)]
    g = [[False] * n for _ in range(n)]
    for a, b in ab:
        g[a - 1][b - 1] = True
        g[b - 1][a - 1] = True

    ans = 0

    for i in range(m):
        visited = [False for _ in range(n)]
        a, b = ab[i]
        g[a - 1][b - 1] = False
        g[b - 1][a - 1] = False

        dfs(g, 0, visited)

        check = False
        for v in visited:
            if not v:
                check = True
        if check:
            ans += 1

        g[a - 1][b - 1] = True
        g[b - 1][a - 1] = True

    print(ans)


if __name__ == "__main__":
    main()
Problem p03575 (Python)

Buggy submission (status: Runtime Error):
def main():
    n, m, *ab = map(int, open(0).read().split())
    e = [set() for _ in range(n)]
    for u, v in zip(*[iter(ab)] * 2):
        e[u - 1].add(v - 1)
        e[v - 1].add(u - 1)

    ans = 0
    for _ in range(m):
        count = 0
        for i, x in enumerate(e):
            if len(x) == 1:
                e[i] = []
                e[x[0]] -= {i}
                count += 1
        if count:
            ans += count
        else:
            print(ans)
            exit()


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, m, *ab = map(int, open(0).read().split())
    e = [set() for _ in range(n)]
    for u, v in zip(*[iter(ab)] * 2):
        e[u - 1].add(v - 1)
        e[v - 1].add(u - 1)

    ans = 0
    for _ in range(m):
        count = 0
        for i, x in enumerate(e):
            if len(x) == 1:
                j = x.pop()
                e[j] -= {i}
                count += 1
        if count:
            ans += count
        else:
            print(ans)
            exit()


if __name__ == "__main__":
    main()
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, n) for (int i = n; i >= 1; i--)
#define mrep(i, from, n) for (int i = from; i < n; i++)
#define mreps(i, from, n) for (int i = from; i <= n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ERASE(x, val) x.erase(remove(ALL(x), val), x.end())
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
ll MOD = 1000000007;
int INF = 1001001001;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  rep(i, m) cin >> a[i] >> b[i];
  int ans = 0;
  rep(i, m) {
    vector<vector<bool>> d(n, vector<bool>(n, false));
    rep(j, m) if (i != j) {
      d[a[j] - 1][b[j] - 1] = true;
      d[b[j] - 1][a[j] - 1] = true;
    }
    rep(z, n) rep(x, n) rep(y, n) if (d[x][z] && d[z][y]) d[x][y] = true;
    rep(j, n) {
      bool ok = true;
      rep(k, n) if (!d[j][k]) ok = false;
      if (!ok) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, n) for (int i = n; i >= 1; i--)
#define mrep(i, from, n) for (int i = from; i < n; i++)
#define mreps(i, from, n) for (int i = from; i <= n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ERASE(x, val) x.erase(remove(ALL(x), val), x.end())
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
ll MOD = 1000000007;
int INF = 1001001001;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  rep(i, m) cin >> a[i] >> b[i];
  int ans = 0;
  rep(i, m) {
    vector<vector<bool>> d(n, vector<bool>(n, false));
    rep(j, m) if (i != j) {
      d[a[j] - 1][b[j] - 1] = true;
      d[b[j] - 1][a[j] - 1] = true;
    }
    rep(z, n) rep(x, n) rep(y, n) if (d[x][z] && d[z][y]) d[x][y] = true;
    rep(j, n) {
      bool ok = true;
      rep(k, n) if (!d[j][k]) ok = false;
      if (!ok) {
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
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につける
  }
  void rereset(int n) {
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  void reset(int n) {
    for (int i = 0; i < n; i++)
      par[i] = root(par[i]);
  }
  bool solve(int n) {
    int num = 0;
    for (int i = 0; i < n; i++)
      if (par[i] == par[0])
        num++;
    return num == n;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  pair<int, int> p[n];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    p[i] = make_pair(a, b);
  }
  UnionFind tree(n);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    tree.rereset(n);
    for (int j = 0; j < m; j++)
      if (j != i)
        tree.unite(p[j].first, p[j].second);
    tree.reset(n);
    ans += (1 - tree.solve(n));
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
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につける
  }
  void rereset(int n) {
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  void reset(int n) {
    for (int i = 0; i < n; i++)
      par[i] = root(par[i]);
  }
  bool solve(int n) {
    int num = 0;
    for (int i = 0; i < n; i++)
      if (par[i] == par[0])
        num++;
    return num == n;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  pair<int, int> p[m];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    p[i] = make_pair(a, b);
  }
  UnionFind tree(n);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    tree.rereset(n);
    for (int j = 0; j < m; j++)
      if (j != i)
        tree.unite(p[j].first, p[j].second);
    tree.reset(n);
    ans += (1 - tree.solve(n));
  }
  cout << ans;
  return 0;
}
Problem p03575 (C++)

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

#define INF 1100000
using namespace std;

int par[110000];
int rnk[110000];

void init(int n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
    rnk[i] = 0;
  }
}

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (rnk[x] < rnk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rnk[x] == rnk[y])
      rnk[x]++;
  }
}

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

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

  vector<pair<int, int>> e(N);
  for (int i = 0; i < M; i++) {
    cin >> e[i].first >> e[i].second;
  }

  int ans = 0;
  for (int i = 0; i < M; i++) {
    init(N);
    for (int j = 0; j < M; j++) {
      if (j == i)
        continue;
      unite(e[j].first, e[j].second);
    }
    if (same(e[i].first, e[i].second) == false)
      ans++;
  }
  cout << ans << endl;

  return 0;
}


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

#define INF 1100000
using namespace std;

int par[110000];
int rnk[110000];

void init(int n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
    rnk[i] = 0;
  }
}

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (rnk[x] < rnk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rnk[x] == rnk[y])
      rnk[x]++;
  }
}

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

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

  vector<pair<int, int>> e(M);
  for (int i = 0; i < M; i++) {
    cin >> e[i].first >> e[i].second;
  }

  int ans = 0;
  for (int i = 0; i < M; i++) {
    init(N);
    for (int j = 0; j < M; j++) {
      if (j == i)
        continue;
      unite(e[j].first, e[j].second);
    }
    if (same(e[i].first, e[i].second) == false)
      ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p03575 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int N, M;
bool graph[50][50];
bool visited[50];

void dfs(int v) {
  visited[v] = true;
  rep(i, N) {
    if (graph[v][i] == false)
      continue;
    if (visited[i] == true)
      continue;
    dfs(i);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  int a[N], b[N];
  rep(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    graph[a[i]][b[i]] = true;
    graph[b[i]][a[i]] = true;
  }
  int ans = 0;
  rep(i, M) {
    rep(j, N) visited[j] = false;
    bool bridge = false;

    graph[a[i]][b[i]] = false;
    graph[b[i]][a[i]] = false;

    dfs(0);
    rep(j, N) if (visited[j] == false) bridge = true;
    if (bridge)
      ans++;

    graph[a[i]][b[i]] = true;
    graph[b[i]][a[i]] = true;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int N, M;
bool graph[50][50];
bool visited[50];

void dfs(int v) {
  visited[v] = true;
  rep(i, N) {
    if (graph[v][i] == false)
      continue;
    if (visited[i] == true)
      continue;
    dfs(i);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  int a[M], b[M];
  rep(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    graph[a[i]][b[i]] = true;
    graph[b[i]][a[i]] = true;
  }
  int ans = 0;
  rep(i, M) {
    rep(j, N) visited[j] = false;
    bool bridge = false;

    graph[a[i]][b[i]] = false;
    graph[b[i]][a[i]] = false;

    dfs(0);
    rep(j, N) if (visited[j] == false) bridge = true;
    if (bridge)
      ans++;

    graph[a[i]][b[i]] = true;
    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;
using ll = long long;
template <typename T> class UnionFind {
public:
  vector<T> par;
  UnionFind(T n) : par(n, -1) {}
  void init(T n) {
    for (ll i = 0; i < n; i++) {
      par[i] = -1;
    }
  }

  ll root(T x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

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

  bool merge(T x, T y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  ll size(T x) { return -par[root(x)]; }
};

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n, m;
  cin >> n >> m;
  vector<pair<ll, ll>> edges(m);
  for (ll i = 0; i < m; i++) {
    cin >> edges[i].first >> edges[i].second;
    edges[i].first--;
    edges[i].second--;
  }
  ll ans = 0;
  for (ll i = 0; i < m; i++) {
    UnionFind<ll> uft(n);
    uft.init(n);
    for (ll j = 0; j < m; j++) {
      if (j == i) {

      } else {
        uft.merge(edges[j].first, edges[j].second);
      }
    }
    if (uft.size(i) != n)
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T> class UnionFind {
public:
  vector<T> par;
  UnionFind(T n) : par(n, -1) {}
  void init(T n) {
    for (ll i = 0; i < n; i++) {
      par[i] = -1;
    }
  }

  ll root(T x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

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

  bool merge(T x, T y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  ll size(T x) { return -par[root(x)]; }
};

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n, m;
  cin >> n >> m;
  vector<pair<ll, ll>> edges(m);
  for (ll i = 0; i < m; i++) {
    cin >> edges[i].first >> edges[i].second;
    edges[i].first--;
    edges[i].second--;
  }
  ll ans = 0;
  for (ll i = 0; i < m; i++) {
    UnionFind<ll> uft(n);
    uft.init(n);
    for (ll j = 0; j < m; j++) {
      if (j == i) {

      } else {
        uft.merge(edges[j].first, edges[j].second);
      }
    }
    if (uft.size(0) != n)
      ans++;
  }
  cout << ans << endl;
}