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

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> G(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int cnt = 0;
  vector<int> dist(n, -1);
  rep(sv, n) {
    if (dist[sv] != -1)
      continue;
    queue<int> q;
    dist[sv] = 0;
    q.push(sv);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int nv : G[v]) {
        if (dist[nv] != -1)
          continue;
        dist[nv] = dist[v] + 1;
        q.push(nv);
      }
    }
    cnt++;
  }
  int ans = cnt - 1;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

struct UnionFind {
  vector<int> d;
  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);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }
  int cnt = 0;
  rep(i, n) {
    if (!uf.same(i, i + 1))
      cnt++;
  }
  cnt--;
  cout << cnt << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 1e7
vector<ll> g[100005];
ll vis[100005];

void dfs(ll u, ll pt) {
  vis[u] = 1;
  for (auto it : g[u]) {
    if (it == pt || vis[it])
      continue;
    dfs(it, u);
  }
}
void solve() {
  ll n, m;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    g[i].clear();
    vis[i] = 0;
  }
  // vector<ll> g[n+1];
  for (int i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    // cout<<"next0";
    g[a].pb(b);
    g[b].pb(a);
  }
  // cout<<"hi0\\n";
  ll cnt = 0;
  for (ll i = 1; i <= n; i++) {

    if (vis[i] == 0) { // cout<<"his";
      dfs(i, -1);
      // cout<<"hi\\n";
      cnt++;
    }
  }
  // cout<<cnt;
  if (cnt == 0) {
    cout << "0\\n";
    return;
  }
  cout << cnt - 1 << '\\n';
}
int main() {
  // IOS;
  int tt;
  tt = 1;
  // cin>>tt;
  int i = 1;
  while (tt--) {
    // cout<<"tc "<<i<<" ";
    solve();
    i++;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 1e7
vector<ll> g[100005];
ll vis[100005];
ll n, m;
void dfs(ll u, ll pt) {
  vis[u] = 1;
  for (auto it : g[u]) {
    if (it == pt)
      continue;
    dfs(it, u);
  }
}
void solve() {
  // ll n,m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    g[i].clear();
    vis[i] = 0;
  }
  // vector<ll> g[n+1];
  while (m--) {
    ll a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      dfs(i, -1);
      cnt++;
    }
  }
  cout << cnt - 1 << '\\n';
}
int main() {
  IOS;
  int tt;
  tt = 1;
  // cin>>tt;
  int i = 1;
  while (tt--) {
    // cout<<"tc "<<i<<" ";
    solve();
    i++;
  }
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define M (L + R) / 2
#define N 500009
#define inf 99999999999
#define ll long long
#define mod 1000000007
#define sz size();
using namespace std;
ll n, m, a, b, parent[N], child[N];

ll Parent(ll x) {
  if (x != parent[x])
    parent[x] = Parent(parent[x]);
  return parent[x];
}

void merge(ll x, ll y) {
  x = Parent(x);
  y = Parent(y);
  if (x == y)
    return;
  if (child[x] < child[y])
    swap(x, y);
  parent[y] = x;
  child[x] += child[y];
  return;
}

int main() {

  ios::sync_with_stdio(0);

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    child[i] = 1;
    parent[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    merge(a, b);
  }
  ll ans = -1;
  for (int i = 1; i <= n; i++) {
    if (child[Parent(i)] != 0)
      ans++, child[Parent(i)] = 0;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define M (L + R) / 2
#define N 500009
#define inf 99999999999
#define ll long long
#define mod 1000000007
#define sz size();
using namespace std;
ll n, m, a, b, parent[N], child[N];

ll Parent(ll x) {
  if (x != parent[x]) {
    parent[x] = Parent(parent[x]);
    return parent[x];
  }
}

void merge(ll x, ll y) {
  if (Parent(x) == Parent(y))
    return;
  if (child[x] < child[y])
    swap(x, y);
  parent[y] = x;
  child[x] += child[y];
  return;
}

int main() {

  ios::sync_with_stdio(0);

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    child[i] = 1;
    parent[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    merge(a, b);
  }
  //	ll ans=0;
  //	for(int i=1;i<=n;i++){
  //		if(child[parent[i]]!=0)ans++,child[parent[i]]=0;
  //	}
  //	cout<<ans-1<<endl;
  ll ans = max(n - 1 - m, 0ll);
  cout << ans << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long int
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define all(x) (x).begin(), (x).end()

void dfs(vector<vector<ll>> &v, ll p, vector<bool> &vis) {

  vis[p] = true;

  for (auto it = v[p].begin(); it != v[p].end(); ++it) {
    if (vis[*it] == false)
      dfs(v, *it, vis);
  }
}

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

  vector<vector<ll>> v(n + 1);

  for (int i = 1; i <= m; i++) {
    ll x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  vector<bool> vis(n + 1, false);

  ll ans = 0;

  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    dfs(v, i, vis);
    ans++;
  }

  cout << ans - 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long int
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define all(x) (x).begin(), (x).end()

void dfs(vector<vector<ll>> v, ll p, vector<bool> &vis) {

  vis[p] = true;

  for (auto it = v[p].begin(); it != v[p].end(); ++it) {
    if (vis[*it] == false)
      dfs(v, *it, vis);
  }
}

int main() {

  ll n, m;
  cin >> n >> m;

  vector<vector<ll>> v(n + 1);

  for (int i = 1; i <= m; i++) {
    ll x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  vector<bool> vis(n + 1, false);

  ll ans = 0;

  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    dfs(v, i, vis);
    ans++;
  }

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

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

void dfs(int source, vector<int> adj[], vector<bool> &visited) {
  visited[source] = true;

  for (auto v : adj[source]) {
    if (!visited[v]) {
      dfs(v, adj, visited);
    }
  }
}

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

  int n = 0, m = 0;
  cin >> n >> m;

  vector<int> adj[n + 1];

  while (m--) {
    int a = 0, b = 0;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  vector<bool> visited(n + 1, false);
  int components = 0;

  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      components++;
      dfs(i, adj, visited);
    }
  }

  cout << components - 1 << "\\n";

  return 0;
}


Error: 0

Fixed submission:

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

void dfs(int source, vector<int> adj[], vector<bool> &visited) {
  visited[source] = true;

  for (auto v : adj[source]) {
    if (!visited[v]) {
      dfs(v, adj, visited);
    }
  }
}

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

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

  vector<int> adj[n];

  while (m--) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  vector<bool> visited(n, false);
  int components = 0;

  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      components++;
      dfs(i, adj, visited);
    }
  }

  cout << components - 1 << '\\n';

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

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int p[N], cnt;
int find(int x) {
  if (x == p[x])
    return x;
  return find(p[x]);
}
void Union(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  p[x] = p[y];
  cnt--;
}
int ans;
bool pa[N];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  cnt = n;
  for (int i = 1; i <= n; i++)
    p[i] = i;
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    Union(a, b);
  }
  printf("%d\\n", cnt - 1);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 400005;
int p[N];
bool bj[N];
struct edge {
  int v, next;
} e[N * 2];
int head[N], eid;
// int find(int x){
//	if (x==p[x]) return x;
//	return find(p[x]);
// }
// void Union(int x,int y){
//	x=find(x);
//	y=find(y);
//	if (x!=y) p[x]=y;
// }
// int ans;
void addEdge(int u, int v) {
  e[eid] = {v, head[u]};
  head[u] = eid++;
}
void dfs(int u, int fa) {
  bj[u] = 1;
  //	cout<<u<<endl;
  for (int i = head[u]; ~i; i = e[i].next) {
    int v = e[i].v;
    //	cout<<v<<endl;
    if (v == fa)
      continue;
    dfs(v, u);
  }
}
bool pa[N];
int main() {
  memset(head, -1, sizeof head);
  //	freopen("sample.txt","r",stdin);
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    addEdge(a, b);
    addEdge(b, a);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (bj[i] == 0) {
      cnt++;
      dfs(i, 0);
    }
  }
  printf("%d\\n", cnt - 1);
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

int par[100010];

int parent(int i) {
  if (par[i] == i)
    return i;
  else {
    return par[i] = parent(par[i]);
  }
}

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

  for (int i = 0; i < N; i++)
    par[i] = i;

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    int pa = parent(A), pb = parent(B);
    if (pa == pb)
      continue;
    else {
      if (pa > pb) {
        par[pa] = pb;
      } else {
        par[pb] = pa;
      }
    }
  }

  set<int> S;
  for (int i = 0; i < N; i++) {
    S.insert(parent(par[i]));
  }
  cout << S.size() - 1 << endl;
}

Error: 0

Fixed submission:

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

int par[100010];

int parent(int i) {
  if (par[i] == i)
    return i;
  else {
    return par[i] = parent(par[i]);
  }
}

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

  for (int i = 0; i < N; i++)
    par[i] = i;

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    int pa = par[A], pb = par[B];
    if (pa != pb) {
      par[pa] = pb;
    }
  }

  set<int> S;
  for (int i = 0; i < N; i++) {
    S.insert(parent(par[i]));
  }
  cout << S.size() - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)

struct UnionFind {
  vector<int> par; // 親の番号
  UnionFind(int size) : par(size, -1) {}
  // xとyの木を併合
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (par[y] < par[x])
        swap(x, y);
      par[x] += par[y];
      par[y] = x;
      return true;
    } else {
      return false;
    }
  }
  // 2つのデータx, yが属する木が同じならtrueを返す
  bool same(int x, int y) { return root(x) == root(y); }
  // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
  int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
  // 頂点 v が属するグループと同じグループに属する頂点数を得る
  int size(int x) { return -par[root(x)]; }
};

void solve() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  // N個の島に対して、N-1個の橋で全て連結できる
  int ans = N - 1;
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    ans -= uf.unite(a, b);
  }
  cout << ans << endl;
}

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)

struct UnionFind {
  vector<int> par; // 親の番号
  UnionFind(int size) : par(size, -1) {}
  // xとyの木を併合
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (par[y] < par[x])
        swap(x, y);
      par[x] += par[y];
      par[y] = x;
      return true;
    } else {
      return false;
    }
  }
  // 2つのデータx, yが属する木が同じならtrueを返す
  bool same(int x, int y) { return root(x) == root(y); }
  // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
  int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
  // 頂点 v が属するグループと同じグループに属する頂点数を得る
  int size(int x) { return -par[root(x)]; }
};

void solve() {
  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 = 0;
  REP(i, N) {
    REP(j, i) {
      // 同じグループでなければ接続
      if (uf.root(i) != uf.root(j)) {
        uf.unite(i, j);
        ans++;
      }
    }
  }
  cout << ans << endl;
}

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  solve();
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> a[100005];
vector<bool> b(100005, false);

void c(int i) {
  if (!b[i]) {
    b[i] = true;
    for (auto v : a[i]) {
      c(v);
    }
  }
}

void solve() {
  int n, m;
  int cnt = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int aa, bb;
    cin >> aa >> bb;
    aa--;
    bb--;
    a[aa].push_back(bb);
    a[bb].push_back(aa);
  }

  for (int i = 0; i < n; i++) {
    if (!b[i]) {
      cnt++;
      c(i);
    }
  }
  cout << cnt - 1 << endl;
}

int main() { solve(); }

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  vector<int> c(n, 0);
  for (int i = 0; i < m; i++) {
    cin >> a.at(i) >> b.at(i);
  }

  for (int i = 0; i < m; i++) {
    int aa, bb;
    aa = a.at(i);
    bb = b.at(i);
    c.at(aa)++;
    c.at(bb)++;
  }
  int count = 0;
  for (int i = 0; i < m; i++) {
    if (c.at(i) == 0)
      count++;
  }
  if (count == 1) {
    cout << count << endl;
  } else {
    cout << count - 1 << endl;
  }
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

int cities, roads, a, b, group;
vector<int> adj[100100];
bool visited[100100];

void dfs(int c) {
  if (!visited[c]) {
    visited[c] = true;
    for (int i = 0; i < adj[c].size(); i++) {
      int d = adj[c][i];
      dfs(d);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> cities >> roads;
  for (int i = 1; i <= roads; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= cities; i++) {
    if (!visited[i])
      group++;
    dfs(i);
  }
  cout << group - 1 << endl;
}


Error: 0

Fixed submission:

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

int cities, roads, a, b, group;
vector<int> adj[10100];
bool visited[100100];

void dfs(int c) {
  if (!visited[c]) {
    visited[c] = true;
    for (int i = 0; i < adj[c].size(); i++) {
      int d = adj[c][i];
      dfs(d);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> cities >> roads;
  for (int i = 1; i <= roads; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= cities; i++) {
    if (!visited[i])
      group++;
    dfs(i);
  }
  cout << group - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
vector<int> rec;
int find(int u) { return u == rec[u] ? u : rec[u] = find(rec[u]); }
int main() {
  int n, m;
  cin >> n >> m;
  rec.resize(n);
  for (int i = 0; i < n; ++i) {
    rec[i] = i;
  }
  int c = n - 1;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    int _u = find(u), _v = find(v);
    if (_u != _v) {
      rec[_v] = _u;
      --c;
    }
  }
  cout << c << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> rec;
int find(int u) { return u == rec[u] ? u : rec[u] = find(rec[u]); }
int main() {
  int n, m;
  cin >> n >> m;
  rec.resize(n);
  for (int i = 0; i < n; ++i) {
    rec[i] = i;
  }
  int c = n - 1;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    int _u = find(u), _v = find(v);
    if (_u != _v) {
      rec[_v] = _u;
      --c;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define int long long int
#define vec vector<int>
#define inf 1e18
using namespace std;
vector<int> adj[100001];
int vis[100001];
void dfs(int node) {
  vis[node] = 1;
  for (int ch : adj[node]) {
    if (!vis[ch])
      dfs(ch);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tt = 1;
  // cin>>tt;
  while (tt--) {
    int n, m, i;
    cin >> n >> m;
    for (i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      adj[u].pb(v);
      adj[v].pb(u);
    }
    int c = 0;
    for (i = 1; i <= n; i++) {
      if (!vis[i])
        dfs(i), c++;
    }
    cout << c - 1;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define int long long int
#define vec vector<int>
#define inf 1e18
using namespace std;
vector<int> adj[100001];
int vis[100001];
int dfs(int node) {
  vis[node] = 1;
  for (int ch : adj[node]) {
    if (!vis[ch])
      dfs(ch);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tt = 1;
  // cin>>tt;
  while (tt--) {
    int n, m, i;
    cin >> n >> m;
    for (i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      adj[u].pb(v);
      adj[v].pb(u);
    }
    int c = 0;
    for (i = 1; i <= n; i++) {
      if (!vis[i])
        dfs(i), c++;
    }
    cout << c - 1;
  }
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 100010;
int n, m, cnt, father[N];

int find(int x) { return x == father[x] ? x : father[x] = find(father[x]); }

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    father[i] = i;
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    father[find(x)] = find(y);
  }
  for (int i = 1; i <= n; i++)
    if (find(i) == i)
      cnt++;
  printf("%d\\n", cnt - 1);
  return 0;
}

Error: 0

Fixed submission:

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

const int N = 100010;
int n, m, cnt, father[N];

int find(int x) { return x == father[x] ? x : father[x] = find(father[x]); }

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    father[i] = i;
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    father[find(x)] = y;
  }
  for (int i = 1; i <= n; i++)
    if (find(i) == i)
      cnt++;
  printf("%d\\n", cnt - 1);
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void dfs(ll i, bool *visited, ll n, vector<ll> *v) {
  visited[i] = true;
  vector<ll> temp = v[i];
  for (int j = 0; j < temp.size(); j++) {
    if (!visited[temp[j]]) {
      dfs(temp[j], visited, n, v);
    }
  }
}

int main() {
  ll n, m;
  // set<int> s;
  cin >> n >> m;
  ll a, b;
  vector<ll> *v = new vector<ll>[n + 1];
  ll count = 0;
  for (ll i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  bool *visited = new bool[n + 1]();
  for (ll i = 1; i <= n; i++) {
    if (visited[i] == false) {
      count++;
      dfs(i, visited, n, v);
    }
  }
  cout << count - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

void dfs(int i, bool *visited, vector<vector<int>> v) {
  visited[i] = true;
  vector<int> temp = v[i];
  for (int j = 0; j < temp.size(); j++) {
    if (!visited[temp[j]]) {
      dfs(temp[j], visited, v);
    }
  }
}

int main() {
  int n, m;
  set<int> s;
  cin >> n >> m;
  int a, b;
  vector<vector<int>> v(n + 1);
  int count = 0;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  bool *visited = new bool[n + 1]();
  for (int i = 1; i <= n; i++) {
    if (visited[i] == false) {
      count++;
      dfs(i, visited, v);
    }
  }
  cout << count - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> graph[1000005];
int color[1000005];

int dfs(int v) {
  color[v] = 1;
  for (auto i : graph[v]) {
    if (color[i] == 0)
      dfs(i);
  }
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  dfs(1);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (color[i] == 0) {
      ans++;
      dfs(i);
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

vector<int> graph[1000005];
int color[1000005];

int dfs(int v) {
  color[v] = 1;
  for (auto i : graph[v]) {
    if (color[i] == 0)
      dfs(i);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  dfs(1);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (color[i] == 0) {
      ans++;
      dfs(i);
    }
  }
  cout << ans << endl;
}