alexjercan/bugnet huggingface dataset:
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

/*Author : Krishna*/
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
using namespace std;

vector<int> adj[100005];
vector<bool> vis(100005, false);
void dfs(int i) {
  if (!vis[i]) {
    vis[i] = true;
    for (auto v : adj[i])
      dfs(v);
  }
}

void solve() {

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  int comp = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      comp++;
      dfs(i);
    }
  }
  cout << comp - 1;
}

int main() {
  int t;
  //    cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}

/*
 *Check for the edge cases(n=1)?
 *Is there any another approach?
 *check for data types
 *Keep on thinking
 */


Error: WA

Fixed submission:

/*Author : Krishna*/
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
using namespace std;

vector<int> adj[100005];
vector<bool> vis(100005, false);
void dfs(int i) {
  if (!vis[i]) {
    vis[i] = true;
    for (auto v : adj[i])
      dfs(v);
  }
}

void solve() {

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    adj[x].push_back(y);
  }
  int comp = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      comp++;
      dfs(i);
    }
  }
  cout << comp - 1;
}

int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}

/*
 *Check for the edge cases(n=1)?
 *Is there any another approach?
 *check for data types
 *Keep on thinking
 */
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

#define endl "\\n"
#define sz(x) (int)(x.size())
#define all(v) v.begin(), v.end()
#define isOdd(n) (n & 1)
#define pow(n, m) (ll) powl(n, m)
#define clr(x, val) memset(x, val, sizeof(x))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1e5 + 2, M = 3e5 + 5;

int ne, head[N], nxt[M], to[M], vis[N];

void initGraph(int n) { // reset graph
  memset(head, -1, n * sizeof(head[0]));
  ne = 0;
}
void addEdge(int f, int t) {
  to[ne] = t;
  nxt[ne] = head[f];
  head[f] = ne++;
}

void addBidirectedEdge(int u, int v) {
  addEdge(u, v);
  addEdge(v, u);
}

void dfs(int u) {
  vis[u] = 1;
  for (int k = head[u]; ~k; k = nxt[k]) {
    int v = to[k];
    if (!vis[v])
      dfs(v);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n, m;
  cin >> n >> m;

  initGraph(n);

  while (m--) {
    int u, v;
    cin >> u >> v;
    v--, u--;
    addBidirectedEdge(u, v);
  }
  int comp = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i);
      comp++;
    }
  }

  cout << comp - 1;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define endl "\\n"
#define sz(x) (int)(x.size())
#define all(v) v.begin(), v.end()
#define isOdd(n) (n & 1)
#define pow(n, m) (ll) powl(n, m)
#define clr(x, val) memset(x, val, sizeof(x))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1e5 + 2, M = 1e5 + 5;

int ne, head[N], nxt[M], to[M], vis[N];

void initGraph(int n) { // reset graph
  memset(head, -1, n * sizeof(head[0]));
  ne = 0;
}
void addEdge(int f, int t) {
  to[ne] = t;
  nxt[ne] = head[f];
  head[f] = ne++;
}

void addBidirectedEdge(int u, int v) {
  addEdge(u, v);
  addEdge(v, u);
}

void dfs(int u) {
  vis[u] = 1;
  for (int k = head[u]; ~k; k = nxt[k]) {
    int v = to[k];
    if (!vis[v])
      dfs(v);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n, m;
  cin >> n >> m;
  initGraph(n);

  while (m--) {
    int u, v;
    cin >> u >> v;
    v--, u--;
    addBidirectedEdge(u, v);
  }
  int comp = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i);
      comp++;
    }
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define vv(T) vector<vector<T>>
#define coa                                                                    \\
  cout << ans << endl;                                                         \\
  return 0;
using namespace std;
using ll = long long;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vector<T>>;
using qint = queue<int>;
using sint = stack<int>;
using pii = pair<int, int>;

class UnionFind {
  vector<int> parent;
  // UnionFind木の実装
public:
  UnionFind(int N) : parent(N, -1) {} // コンストラクタ
  int root(int x) {
    // 根を求める。
    if (parent[x] < 0)
      return x;
    return parent[x] = root(parent[x]);
  }
  void unite(int x, int y) {
    // xとyの木を結合する。
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (-parent[rx] > -parent[ry]) {
      // 小さい方の木を大きい方につなげる。
      parent[rx] += parent[ry];
      parent[ry] = rx;
    } else {
      parent[ry] += parent[rx];
      parent[rx] = ry;
    }
    return;
  }
  bool isSame(int x, int y) {
    // 同じ木にあるか?
    return root(x) == root(y);
  }
  int size(int x) {
    // xの木に属する要素はいくつか?
    return -parent[root(x)];
  }
};

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

  UnionFind uf(n);

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

  int ans = -1;
  rep(i, n) {
    int ri = uf.root(i);
    if (ri == i)
      ans++;
  }

  coa;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define vv(T) vector<vector<T>>
#define coa                                                                    \\
  cout << ans << endl;                                                         \\
  return 0;
using namespace std;
using ll = long long;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vector<T>>;
using qint = queue<int>;
using sint = stack<int>;
using pii = pair<int, int>;

class UnionFind {
  vector<int> parent;
  // UnionFind木の実装
public:
  UnionFind(int N) : parent(N, -1) {} // コンストラクタ
  int root(int x) {
    // 根を求める。
    if (parent[x] < 0)
      return x;
    return parent[x] = root(parent[x]);
  }
  void unite(int x, int y) {
    // xとyの木を結合する。
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (-parent[rx] > -parent[ry]) {
      // 小さい方の木を大きい方につなげる。
      parent[rx] += parent[ry];
      parent[ry] = rx;
    } else {
      parent[ry] += parent[rx];
      parent[rx] = ry;
    }
    return;
  }
  bool isSame(int x, int y) {
    // 同じ木にあるか?
    return root(x) == root(y);
  }
  int size(int x) {
    // xの木に属する要素はいくつか?
    return -parent[root(x)];
  }
};

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

  UnionFind uf(n);

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

  int ans = -1;
  rep(i, n) {
    int ri = uf.root(i);
    if (ri == i)
      ans++;
  }

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

#include <bits/stdc++.h>

using namespace std;

#define INF 1e9
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define repv(i, a, n) for (ll i = (n)-1; i >= a; i--)
#define ll long long int
#define endl "\\n"
#define pb push_back

vector<int> adj[1000005];
bool vis[1000005];

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

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

  int n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  int c = 0;
  rep(i, 1, n + 1) {
    if (!vis[i]) {
      c++;
      dfs(i);
    }
  }
  c--;
  cout << c;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1e9
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define repv(i, a, n) for (ll i = (n)-1; i >= a; i--)
#define ll long long int
#define endl "\\n"

vector<int> adj[10005];
int vis[10005];

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

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

  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      c++;
      dfs(i);
    }
  }
  cout << c - 1;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mod 1000000007
#define int long long int
#define phi 1.6180339
#define rep(i, n) for (auto i = 0; i < (n); i++)
#define endl "\\n"
#define For(i, a, b) for (auto i = (a); i < (b); i++)
#define op1 cin.tie(NULL)
#define op2 cout.tie(NULL)
#define op3 std::ios_base::sync_with_stdio(false)
#define MAXX 100001
using namespace std;
vector<int> edges[MAXX];
bool visited[MAXX];
void dfs(int sv) {
  visited[sv] = true;
  for (auto i : edges[sv])
    if (!visited[i])
      dfs(i);
}
void trozen() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    edges[x].push_back(y);
    edges[y].push_back(x);
  }
  memset(visited, false, sizeof(visited));
  int cnt = -1;
  for (int i = 1; i <= n; i++)
    if (!visited[i]) {
      dfs(i);
      cnt++;
    }
  cout << cnt << endl;
}
int32_t main() {
  op1;
  op2;
  op3;
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif*/
  trozen();
  return 0;
}

Error: TLE

Fixed submission:

/*
        @uthor: Kashish Gilhotra
        user: CodeChef, CodeForces, HackerEarth, HackerRank, SPOJ: kashish001
*/

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

#define int long long int
typedef vector<int> vi;
typedef vector<pair<int, int>> vpi;
typedef vector<vi> vvi;
const int mod = 1e9 + 7;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define debug(...) cerr << "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define EB emplace_back
#define ALL(v) v.begin(), v.end()
#define size(v) (int)v.size()
#define endl "\\n"
#define UMO unordered_map
#define USO unordered_set
#define TC                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

void dfs(vvi in, int n, int sv, vector<bool> &visited) {
  visited[sv] = true;
  for (auto i : in[sv]) {
    if (!visited[i]) {
      dfs(in, n, i, visited);
    }
  }
}

void Panda() {
  int v, e;
  cin >> v >> e;
  vvi in(v);
  for (int i = 0, x, y; i < e; i++) {
    cin >> x >> y;
    x--, y--;
    in[x].EB(y), in[y].EB(x);
  }
  vector<bool> visited(v);
  int cnt = 0;
  for (int i = 0; i < v; i++)
    if (!visited[i]) {
      dfs(in, v, i, visited);
      cnt++;
    }
  cout << cnt - 1 << endl;
}

int32_t main() {

  FAST;

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  Panda();

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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> par;
  vector<int> sz;
  map<int, int> szmp; // map<size, cnt>

  // n : 要素数
  UnionFind(int n = 0) {
    if (n > 0)
      initialize(n);
  }

  void initialize(int n) {
    par.resize(n);
    sz.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
    szmp[1] = n;
  }

  // 0-index
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  // 0-index
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y])
      swap(x, y);

    for (int s : {sz[x], sz[y]}) {
      szmp[s]--;
      if (szmp[s] == 0)
        szmp.erase(s);
    }
    par[x] = y;
    sz[y] += sz[x];
    szmp[sz[y]]++;
  }

  // 0-index
  bool same(int x, int y) { return find(x) == find(y); }

  // 0-index
  int size(int x) { return sz[find(x)]; }

  // 最小要素サイズ
  int minsz() { return szmp.begin()->first; }

  // 連結成分個数
  int unitenum() { return szmp.size(); }
};

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

  UnionFind uf(n);

  while (m--) {
    int a, b;
    cin >> a >> b;
    uf.unite(a - 1, b - 1);
  }
  int cnt = 0;
  for (auto u : uf.szmp) {
    cnt += u.second;
  }
  cout << cnt - 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> par;
  vector<int> sz;
  map<int, int> szmp; // map<size, cnt>

  // n : 要素数
  UnionFind(int n = 0) {
    if (n > 0)
      initialize(n);
  }

  void initialize(int n) {
    par.resize(n);
    sz.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
    szmp[1] = n;
  }

  // 0-index
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  // 0-index
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y])
      swap(x, y);

    for (int s : {sz[x], sz[y]}) {
      szmp[s]--;
      if (szmp[s] == 0)
        szmp.erase(s);
    }
    par[x] = y;
    sz[y] += sz[x];
    szmp[sz[y]]++;
  }

  // 0-index
  bool same(int x, int y) { return find(x) == find(y); }

  // 0-index
  int size(int x) { return sz[find(x)]; }

  // 最小要素サイズ
  int minsz() { return szmp.begin()->first; }

  // 連結成分個数
  int unitenum() { return szmp.size(); }
};

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

  UnionFind uf(n);

  while (m--) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }
  cout << uf.unitenum() - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long int
#define ull unsigned long long int
#define ld long double

int n, m, ans = 0;
vector<vector<int>> g(200010);
bool vis[200010];

void dfs(int v) {
  vis[v] = true;
  for (int i = 0; i < g[v].size(); i++) {
    if (!vis[g[v][i]]) {
      dfs(g[v][i]);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ans += 1;
      dfs(i);
    }
  }
  cout << ans - 1;
  return 0;
}

Error: TLE

Fixed submission:

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

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long int
#define ull unsigned long long int
#define ld long double

int n;
vector<vector<int>> arr(n + 1);
vector<bool> vis(n + 1);
int cnt;

void dfs(int v) {
  vis[v] = true;
  for (int i = 1; i < n + 1; i++) {
    if (vis[i] == false && arr[v][i] == 1) {
      dfs(i);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int m, a, b;
  cin >> n >> m;
  for (int i = 1; i < n + 1; i++) {
    vector<int> ar(n + 1, 0);
    arr.pb(ar);
    vis[i] = false;
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    arr[a][b] = 1;
    arr[b][a] = 1;
  }
  cnt = 0;
  for (int i = 1; i < n + 1; i++) {
    if (!vis[i]) {
      cnt += 1;
      dfs(i);
    }
  }
  cout << cnt - 1;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

class UnionFind {
public:
  explicit UnionFind(int N) : root(N, -1), size(N, 1) {}
  int getRoot(int u) { return root[u] == -1 ? u : root[u] = getRoot(root[u]); }
  int getSize(int u) { return size[getRoot(u)]; }
  bool same(int a, int b) { return getRoot(a) == getRoot(b); }
  bool merge(int a, int b) {
    int u = getRoot(a);
    int v = getRoot(b);
    if (u == v)
      return false;
    root[u] = v;
    size[v] += size[u];
    return true;
  }

private:
  vector<int> root;
  vector<int> size;
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    uf.merge(a, b);
  }
  int res = -1;
  for (int i = 0; i < N; i++)
    if (uf.getRoot(i) == i)
      ++res;
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

class UnionFind {
public:
  explicit UnionFind(int N) : root(N, -1), size(N, 1) {}
  int getRoot(int u) { return root[u] == -1 ? u : root[u] = getRoot(root[u]); }
  int getSize(int u) { return size[getRoot(u)]; }
  bool same(int a, int b) { return getRoot(a) == getRoot(b); }
  bool merge(int a, int b) {
    int u = getRoot(a);
    int v = getRoot(b);
    if (u == v)
      return false;
    root[u] = v;
    size[v] += size[u];
    return true;
  }

private:
  vector<int> root;
  vector<int> size;
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    uf.merge(a, b);
  }
  int res = -1;
  for (int i = 0; i < N; i++)
    if (uf.getRoot(i) == i)
      ++res;
  cout << res << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct union_find {
  vector<int> r;
  union_find(int n) : r(n, -1) {}
  int root(int a) {
    if (r[a] < 0) {
      return a;
    } else {
      return root(r[a]);
    }
  }
  void add(int a, int b) {
    int ra = root(a);
    int rb = root(b);
    if (r[ra] > r[rb]) {
      swap(ra, rb);
    } else if (ra == rb) {
      return;
    }
    r[ra] += r[rb];
    r[rb] = ra;
  }
  int sum() {
    int s = 0;
    for (int i = 0; i < r.size(); i++) {
      if (r[i] < 0) {
        s++;
      }
    }
    return s;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  union_find u(n);
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    u.add(a, b);
  }
  cout << u.sum() - 1 << endl;
}

Error: TLE

Fixed submission:

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

struct union_find {
  vector<int> r;
  union_find(int n) : r(n, -1) {}
  int root(int a) {
    if (r[a] < 0) {
      return a;
    } else {
      return root(r[a]);
    }
  }
  void add(int a, int b) {
    int ra = root(a);
    int rb = root(b);
    if (r[ra] > r[rb]) {
      swap(ra, rb);
    }
    r[ra] += r[rb];
    r[rb] = ra;
  }
  int sum() {
    int s = 0;
    for (int i = 0; i < r.size(); i++) {
      if (r[i] < 0) {
        s++;
      }
    }
    return s;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  union_find u(n);
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--;
    b--;
    u.add(a, b);
  }
  cout << u.sum() - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
const int N = 1e9 + 7;
#define int long long
#define double long double
#define pb push_back
#define vi vector<int>
#define rep(i, n) for (int i = 1; i <= n; i++)
vi ar[100001];
int vis[100001];
void dfs(int node) {
  vis[node] = 1;
  for (int child : ar[node]) {
    if (!vis[child])
      dfs(child);
  }
}
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  int n, m, a, b;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    ar[a].pb(b), ar[b].pb(a);
  }
  int cc = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cc++;
      dfs(i);
    }
  }
  cout << cc - 1;

  return 0;
}


Error: -11

Fixed submission:

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

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
const int N = 1e9 + 7;
#define int long long
#define double long double
#define pb push_back
#define vi vector<int>
#define rep(i, n) for (int i = 1; i <= n; i++)
vi ar[100001];
int vis[100000];
void dfs(int node) {
  vis[node] = 1;
  for (int child : ar[node]) {
    if (!vis[child])
      dfs(child);
  }
}
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  int n, m, a, b;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    ar[a].pb(b), ar[b].pb(a);
  }
  int cc = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cc++;
      dfs(i);
    }
  }
  cout << cc - 1;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using p = pair<int, int>;
const long long INF = 1ll << 60;
using Graph = vector<vector<int>>;

struct UnionFind {
  vector<int> d;
  // UnionFind(int n=0): d(n,-1) {}
  UnionFind(int n) { d.resize(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)]; }
};

set<int> st;

int main() {
  int N, M, A, B;
  cin >> N >> M;
  UnionFind uf(N);
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    A--;
    B--;
    uf.unite(A, B);
  }
  for (int i = 0; i < N; i++) {
    int p;
    p = uf.find(i);
    st.insert(p);
  }

  int ans = 0;
  int s = st.size();
  if (s >= 2)
    ans = s - 1;

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using p = pair<int, int>;
const long long INF = 1ll << 60;
using Graph = vector<vector<int>>;

struct UnionFind {
  vector<int> d;
  // UnionFind(int n=0): d(n,-1) {}
  UnionFind(int n) { d.resize(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, A, B;
  cin >> N >> M;
  UnionFind uf(N);
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    uf.unite(A, B);
  }
  int ans = 0;
  set<int> st;
  rep(i, N) { st.insert(uf.find(i)); }
  ans = st.size() / 2 + st.size() % 2;

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