alexjercan/bugnet huggingface dataset:
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
struct aaa {
  aaa() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} aaa;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
#define debug(v)                                                               \\
  { cerr << "\\033[1;36m[debug]\\033[m " << #v << " : " << v << endl; }

using int64 = long long;

void dfs(const vector<vector<int>> &g, const int &i, vector<bool> &visited) {
  if (visited[i])
    return;
  visited[i] = true;

  for (auto v : g[i]) {
    if (visited[v])
      continue;
    dfs(g, v, visited);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n + 1);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  int ans = 0;
  vector<bool> visited(n + 1, false);
  for (int i = 1; i <= n; i++) {
    if (visited[i])
      continue;
    dfs(g, i, visited);
    ans++;
  }

  cout << ans - 1 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
struct aaa {
  aaa() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} aaa;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
#define debug(v)                                                               \\
  { cerr << "\\033[1;36m[debug]\\033[m " << #v << " : " << v << endl; }

using int64 = long long;

void dfs(const vector<vector<int>> &g, const int &i, vector<bool> &visited) {
  if (visited[i])
    return;
  visited[i] = true;

  for (auto v : g[i]) {
    if (visited[v])
      continue;
    cerr << i << "=>" << v << endl;
    dfs(g, v, visited);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n + 1);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  int ans = 0;
  vector<bool> visited(n + 1, false);
  for (int i = 1; i <= n; i++) {
    debug(visited);
    if (visited[i])
      continue;
    dfs(g, i, visited);
    ans++;
  }

  cout << ans - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> g[100001];
bool vis[100001];

void dfs(int src) {
  vis[src] = true;
  for (auto child : g[src]) {
    if (!vis[child]) {
      dfs(child);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 1; i <= n; i++) {
    vis[i] = false;
  }

  int cc = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      cc++;
    }
  }
  cout << cc - 1 << endl;
}

Error: -11

Fixed submission:

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

vector<int> g[100001];
vector<bool> vis;

void dfs(int src) {
  vis[src] = true;
  for (auto child : g[src]) {
    if (!vis[child]) {
      dfs(child);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 1; i <= n; i++) {
    vis[i] = false;
  }

  int cc = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  cout << cc << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> d;
  UnionFind(int n) : 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) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    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;
  UnionFind uf(N);

  int ans = N - 1;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--, B--;
    if (!uf.same(A, B)) {
      uf.unite(A, B);
      ans--;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> d;
  UnionFind(int n) : 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) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    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;
  UnionFind uf(N);
  vector<int> A(N), B(N);
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--, B[i]--;
    uf.unite(A[i], B[i]);
  }
  int ans = N - 1;
  for (int i = 0; i < M; i++) {
    if (uf.same(A[i], B[i]))
      ans--;
  }
  cout << ans << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

vector<vector<int>> adj;
vector<int> vis;

void dfs(int n) {
  vis[n] = 1;
  for (auto i : adj[n]) {
    if (vis[i] != 1)
      dfs(i);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  adj.resize(n + 1, vector<int>());
  vis.resize(n + 1, 0);
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] != 1) {
      ans++;
      dfs(i);
    }
  }
  cout << ans - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

void DFS(vector<vector<bool>> friends, int n, vector<bool> &visited, int v) {
  for (int i = 0; i < n; ++i) {

    // A student is in the friend circle if he/she is friends with the student
    // represented by studentIndex and if he/she is not already in a friend
    // circle
    if (friends[v][i] == 1 && !visited[i] && i != v) {
      visited[i] = 1;
      DFS(friends, n, visited, i);
    }
  }
}

int friendCircles(vector<vector<bool>> friends, int n) {
  if (!n) {
    return 0;
  }

  int numCircles = 0; // Number of friend circles

  // Keep track of whether a student is already in a friend circle
  vector<bool> visited(n, 0);

  // Start with the first student and recursively find all other students in
  // his/her friend circle. Then, do the same thing for the next student that is
  // not already in a friend circle. Repeat until all students are in a friend
  // circle.
  for (int i = 0; i < n; ++i) {
    if (!visited[i]) {
      visited[i] = true;
      DFS(friends, n, visited, i); // Recursive step to find all friends
      numCircles++;
    }
  }

  return numCircles;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> f(n + 1, vector<bool>(n, false));
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    f[a - 1][b - 1] = true;
    f[b - 1][a - 1] = true;
  }
  cout << friendCircles(f, n) - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

ll INF = 1ll << 60;

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;

  UnionFind u(n);

  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    u.unite(a, b);
  }

  vector<bool> ck(n, false);

  rep(i, 0, n) { ck[u.root(i)] = true; }

  int cnt = 0;

  rep(i, 0, n) {
    if (ck[i])
      cnt++;
  }

  cout << cnt - 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

ll INF = 1ll << 60;
const int MAX = 200010;
int par[MAX];
bool ck[MAX];

void init() {
  rep(i, 0, MAX) {
    par[i] = -1;
    ck[i] = true;
  }
}

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

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

void unite(int x, int y) {
  int rx = root(x);
  int ry = root(y);
  if (rx == ry) {
    return;
  }
  if (size(x) < size(y)) {
    swap(x, y);
  }
  par[x] += par[y];
  par[y] = x;
  ck[y] = false;
  return;
}

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

  init();

  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    unite(a, b);
  }

  int cnt = 0;

  rep(i, 0, n) {
    if (ck[i])
      cnt++;
  }

  cout << cnt - 1 << endl;

  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Pine1999
 */

#include <bits/stdc++.h>
// use library with "../library/***.hpp"
#include <cassert>
#include <vector>

class unionfind {
public:
  int _n;
  std::vector<int> d;

  unionfind(int n) : _n(n) { d.resize(n, -1); }

  int find(int x) {
    assert(0 <= x && x < _n);
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }

  void unite(int x, int y) {
    assert(0 <= x && x < _n);
    assert(0 <= y && y < _n);
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (d[x] > d[y])
      std::swap(x, y);
    d[x] += d[y];
    d[y] = x;
  }

  bool same(int x, int y) {
    assert(0 <= x && x < _n);
    assert(0 <= y && y < _n);
    return find(x) == find(y);
  }

  int size(int x) {
    assert(0 <= x && x < _n);
    return -d[x];
  }
};

using namespace std;

void CConnectCities(std::istream &in, std::ostream &out) {
  int n, m;
  in >> n >> m;
  int ans = n - 1;
  unionfind uf(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    in >> a >> b;
    --a;
    --b;
    if (!uf.same(a, b)) {
      uf.unite(a, b);
      --ans;
    }
  }
  out << ans << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  CConnectCities(in, out);
  return 0;
}

Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Pine1999
 */

#include <bits/stdc++.h>
#include <cassert>
#include <vector>

class unionfind {
public:
  int _n;
  std::vector<int> d;

  unionfind(int n) : _n(n) { d.resize(n, -1); }

  int find(int x) {
    assert(0 <= x && x < _n);
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }

  void unite(int x, int y) {
    assert(0 <= x && x < _n);
    assert(0 <= y && y < _n);
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (d[x] > d[y])
      std::swap(x, y);
    d[x] += y;
    y = d[x];
  }

  bool same(int x, int y) {
    assert(0 <= x && x < _n);
    assert(0 <= y && y < _n);
    return find(x) == find(y);
  }

  int size(int x) {
    assert(0 <= x && x < _n);
    return -d[x];
  }
};

using namespace std;

void CConnectCities(std::istream &in, std::ostream &out) {
  int n, m;
  in >> n >> m;
  int ans = n - 1;
  unionfind uf(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    in >> a >> b;
    --a;
    --b;
    if (!uf.same(a, b)) {
      uf.unite(a, b);
      --ans;
    }
  }
  out << ans << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  CConnectCities(in, out);
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <atcoder/dsu>
#include <bits/stdc++.h>

#define MOD (long long)(1E9 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace atcoder;
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  dsu d(n);
  vector<int> data(n + 1);
  rep(i, m) {
    cin >> a >> b;
    d.merge(a - 1, b - 1);
  }
  auto c = d.groups();
  cout << c.size() - 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD (long long)(1E9 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  vector<int> data(n + 1);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i] = (a[i] << 17) + b[i];
  }
  sort(a.begin(), a.end());
  rep(i, m) {
    b[i] = (a[i]) & ((1 << 17) - 1);
    a[i] >>= 17;
  }
  int count = 0;
  rep(i, m) {
    if (data[a[i]] == 0) {
      data[a[i]] = data[b[i]] = a[i];
      count++;
    } else {
      data[b[i]] = data[a[i]];
    }
  }
  rep(i, n) {
    if (data[i + 1] == 0)
      count++;
  }
  cout << count - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<vector<int>> adj;
vector<bool> vis;
void dfs(int v) {
  vis[v] = true;
  for (auto it : adj[v]) {
    if (!vis[it])
      dfs(it);
  }
}
int main() {
  int n, m;
  int ans = 0;
  cin >> n >> m;
  adj.resize(n + 1);
  vis.resize(n + 1, false);
  set<int> s;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<vector<int>> adj;
int dfs(int u, int p) {
  int ans = 0;
  for (auto index : adj[u]) {
    if (index != p) {
      int doit = dfs(index, u);
      ans++;
      ans += doit;
    }
  }
  return ans;
}
int main() {
  int n, m;
  int count1 = 0;
  cin >> n >> m;
  adj.resize(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  count1 = dfs(0, -1);
  count1++;
  cout << n - count1;
  adj.clear();
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long n, m;
  cin >> n >> m;
  vector<vector<int>> michi(n, vector<int>(0));
  vector<int> checked(n, 0);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    michi.at(a).push_back(b);
    michi.at(b).push_back(a);
  }
  long long ans = 0;
  int y = 1;
  int l = 0;
  while (y != 0) {
    queue<int> q;
    q.push(l);
    checked.at(l) = 1;
    while (q.size()) {
      int x = q.front();
      q.pop();

      for (int i = 0; i < (int)michi.at(x).size(); i++) {
        int u = michi.at(x).at(i);
        if (checked.at(u) == 1) {
          continue;
        }
        checked.at(u) = 1;
        q.push(u);
      }
    }
    y = 0;
    for (int i = l; i < n; i++) {
      if (checked.at(i) == 0) {
        l = i;
        y = 1;
        // cout << l << endl;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  long long n, m;
  cin >> n >> m;
  vector<vector<int>> michi(n, vector<int>(0));
  vector<int> checked(n, 0);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    michi.at(a).push_back(b);
    michi.at(b).push_back(a);
  }
  long long ans = 0;
  int y = 1;
  int l = 0;
  while (y != 0) {
    queue<int> q;
    q.push(l);
    checked.at(l) = 1;
    while (q.size()) {
      int x = q.front();
      q.pop();

      for (int i = 0; i < (int)michi.at(x).size(); i++) {
        int u = michi.at(x).at(i);
        if (checked.at(u) == 1) {
          continue;
        }
        checked.at(u) = 1;
        q.push(u);
      }
    }
    y = 0;
    for (int i = 0; i < n; i++) {
      if (checked.at(i) == 0) {
        l = i;
        y = 1;
        // cout << l << endl;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
typedef 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;
  }
} UnionFind;

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    --A;
    --B;
    tree.unite(A, B);
  }
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (tree.par[i] == i)
      cnt++;
  }
  cout << cnt - 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

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

int main() {
  int N, M, count = 0;
  cin >> N >> M;
  UnionFind tree(N);
  int R[100000];

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    tree.unite(A, B);
  }
  for (int i = 0; i < N; i++) {
    int x = tree.root(i);
    R[x]++;
  }
  for (int i = 0; i < N; i++) {
    if (R[i])
      count++;
  }
  cout << count - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

/* -> Written by <-
   -----------
  |K_A_Z_A_M_A|
  |___________|
  |    _    |
  |   (^_^)   |
  |  /( | )\\  |
  |____|_|____|
    H O A N G
*/

#define Task ""
#define F first
#define S second
#define push_back pb
#define bit(x, i) (x >> i) & 1
#define inf 1e9 + 7
#define INF 1e18 + 7
#define all(x) x.begin(), x.end()

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;

const int MOD = 998244353;
const double eps = 1e-6;
const int maxn = 1e5 + 5;

int n, m;
vector<int> a[maxn];
int par[maxn];

void dfs(int u) {
  for (auto v : a[u])
    if (v != par[u]) {
      if (par[v])
        continue;
      par[v] = u;
      dfs(v);
    }
}

void Solve() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    a[u].pb(v);
    a[v].pb(u);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (par[i] == 0) {
      cnt++;
      dfs(i);
    }
  cout << cnt - 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  if (fopen(Task ".inp", "r")) {
    freopen(Task ".inp", "r", stdin);
    freopen(Task ".out", "w", stdout);
  }
  int test_case = 1;
  //    cin >> test_case;
  while (test_case--)
    Solve();
  return 0;
}


Error: WA

Fixed submission:

/* -> Written by <-
   -----------
  |K_A_Z_A_M_A|
  |___________|
  |    _    |
  |   (^_^)   |
  |  /( | )\\  |
  |____|_|____|
    H O A N G
*/

#define Task ""
#define F first
#define S second
#define push_back pb
#define bit(x, i) (x >> i) & 1
#define inf 1e9 + 7
#define INF 1e18 + 7
#define all(x) x.begin(), x.end()

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;

const int MOD = 998244353;
const double eps = 1e-6;
const int maxn = 1e5 + 5;

int n, m;
vector<int> a[maxn];
int par[maxn];

void dfs(int u) {
  for (auto v : a[u])
    if (v != par[u]) {
      par[v] = u;
      dfs(v);
    }
}

void Solve() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    a[u].pb(v);
    a[v].pb(u);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (par[i] == 0) {
      cnt++;
      dfs(i);
    }
  cout << cnt - 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  if (fopen(Task ".inp", "r")) {
    freopen(Task ".inp", "r", stdin);
    freopen(Task ".out", "w", stdout);
  }
  int test_case = 1;
  //    cin >> test_case;
  while (test_case--)
    Solve();
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define ll long long
#include <set>
#include <vector>
// vector<ll>adj[100001];
ll parent[100001];
ll tao[100001];
ll find(ll i) {
  if (parent[i] == i)
    return i;
  else
    return parent[i] = find(parent[i]);
}
void union_set(ll a, ll b) {
  ll x = find(a);
  ll y = find(b);
  if (tao[x] < tao[y])
    swap(x, y);
  parent[y] = x;
  if (tao[x] == tao[y])
    tao[x]++;
}
set<ll> s;
int main() {
  ll n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    tao[i] = 1;
  }
  while (m--) {
    ll l, r;
    cin >> l >> r;
    union_set(l, r);
  }
  for (int i = 1; i <= n; i++)
    s.insert(find(i));
  cout << s.size() - 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define ll long long
#include <set>
#include <vector>
// vector<ll>adj[100001];
ll parent[100001];
ll tao[100001];
ll find(ll i) {
  if (parent[i] == i)
    return i;
  else
    return parent[i] = find(parent[i]);
}
ll union_set(ll a, ll b) {
  ll x = find(a);
  ll y = find(b);
  if (tao[x] < tao[y])
    swap(x, y);
  parent[y] = x;
  if (tao[x] == tao[y])
    tao[x]++;
}
set<ll> s;
int main() {
  ll n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    tao[i] = 1;
  }
  while (m--) {
    ll l, r;
    cin >> l >> r;
    union_set(l, r);
  }
  for (int i = 1; i <= n; i++)
    s.insert(find(i));
  cout << s.size() - 1 << endl;

  return 0;
}