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

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n];
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    adj[u - 1].push_back(v - 1);
    adj[v - 1].push_back(u - 1);
  }

  int visited[n], cmp[n], freq[n];
  for (int i = 0; i < n; i++) {
    visited[i] = 0;
    cmp[i] = -1;
    freq[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    if (visited[i])
      continue;
    queue<int> q;
    q.push(i);
    visited[i] = 1;
    cmp[i] = i;
    while (!q.empty()) {
      u = q.front();
      q.pop();
      for (auto v : adj[u]) {
        if (visited[v] == 0) {
          q.push(v);
          visited[v] = 1;
          cmp[v] = i;
        }
      }
    }
  }

  int count = 0;
  for (int i = 0; i < n; i++) {
    if (cmp[i] != -1)
      freq[cmp[i]]++;
  }

  for (int i = 0; i < n; i++) {
    // cout << "cmp and freq " << cmp[i] << " " << freq[i] << endl;
    if (freq[i] > 0)
      count++;
  }

  cout << count - 1 << endl;
}


Error: WA

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n];
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  int visited[n], cmp[n], freq[n];
  for (int i = 0; i < n; i++) {
    visited[i] = 0;
    cmp[i] = 0;
    freq[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    if (visited[i])
      continue;
    queue<int> q;
    q.push(i);
    visited[i] = 1;
    cmp[i] = i;
    while (!q.empty()) {
      u = q.front();
      q.pop();
      for (auto v : adj[u]) {
        if (visited[i] == 0) {
          q.push(v);
          visited[v] = 1;
          cmp[v] = i;
        }
      }
    }
  }

  int count = 0;
  for (int i = 0; i < n; i++) {
    if (cmp[i] != -1)
      freq[cmp[i]]++;
  }

  for (int i = 0; i < n; i++) {
    if (freq[i] > 0)
      count++;
  }

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

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

#define ll long long
#define ar array
const int mxn = 1e5 + 1;
ll n, m;
vector<ll> g[mxn];
vector<bool> used(mxn, false);
int ans = 0;
void dfs(int u) {
  used[u] = true;
  for (ll i : g[u])
    if (!used[i])
      dfs(i);
}
void solve() {
  cin >> n >> m;
  while (m--) {
    ll a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (ll i = 1; i <= n; i++)
    if (!used[i])
      dfs(i), ++ans;
  cout << ans - 1;
}
int main() { solve(); }


Error: WA

Fixed submission:

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

#define ll long long
#define ar array
const int mxn = 1e5;
ll n, m;
vector<ll> g[mxn];
vector<bool> used(mxn, false);
int ans = 0;
void dfs(int u) {
  used[u] = true;
  for (ll i : g[u])
    if (!used[i])
      dfs(i);
}
void solve() {
  cin >> n >> m;
  while (m--) {
    ll a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (ll i = 0; i < n; i++)
    if (!used[i])
      dfs(i), ++ans;
  for (int i = 0; i < n; i++)
    if (!used[i])
      ++ans;
  cout << ans;
}
int main() { solve(); }
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
using namespace std;
#define ll long long
#define vvll vector<vector<long long>>
#define vll vector<long long>
#define fri(i, n) for (int i = 0; i < n; i++)
#define fri1(i, n) for (int i = n - 1; i >= 0; i--)
#define map map<ll, ll>
#define pb push_back
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ump unordered_map<long long, long long>
#define ub upper_bound
#define bsearch binary_search

ll parent[100001] = {0};

int find(int x) {
  if (parent[x] != x) {
    parent[x] = find(parent[x]);
  }
  return parent[x];
}

int main() {
  fast ll n, m, x, y;
  cin >> n >> m;
  fri(i, n + 1) { parent[i] = i; }
  fri(i, m) {
    cin >> x >> y;
    if (find(y) != find(x)) {
      parent[find(y)] = find(x);
    }
  }
  set<int> st, st1;
  for (int i = 1; i <= n; i++) {
    st.insert(parent[i]);
  }
  for (auto it : st) {
    st1.insert(find(it));
  }
  cout << st1.size() - 1 << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
using namespace std;
#define ll long long
#define vvll vector<vector<long long>>
#define vll vector<long long>
#define fri(i, n) for (int i = 0; i < n; i++)
#define fri1(i, n) for (int i = n - 1; i >= 0; i--)
#define map map<ll, ll>
#define pb push_back
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ump unordered_map<long long, long long>
#define ub upper_bound
#define bsearch binary_search

ll parent[100001] = {0};
int find(int x) {
  while (parent[x] != x) {
    parent[x] = find(parent[x]);
  }
  return parent[x];
}
int main() {
  fast ll n, m, x, y;
  cin >> n >> m;
  fri(i, n + 1) { parent[i] = i; }
  fri(i, m) {
    cin >> x >> y;
    if (find(y) != find(x)) {
      parent[y] = find(x);
    }
  }
  set<int> st;
  for (int i = 1; i <= n; i++) {
    st.insert(parent[i]);
  }
  cout << st.size() - 1 << "\\n";
  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;

// UnionFind木
const int MAX = 100010;
int par[MAX];

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

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(rx) < size(ry)) {
    swap(rx, ry);
  }
  par[rx] += par[ry];
  par[ry] = rx;
  return;
}

bool same(int x, int y) {
  int rx = root(x);
  int ry = root(y);
  if (rx < 0 || ry < 0) {
    if (rx >= 0) {
      return rx == y;
    }
    if (ry >= 0) {
      return ry == x;
    }
    return false;
  }
  return rx == ry;
}

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

  init();

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

  vb ck(n, false);

  rep(i, 0, n) ck[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;

// UnionFind木
const int MAX = 100010;
int par[MAX];

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

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

bool same(int x, int y) {
  int rx = root(x);
  int ry = root(y);
  if (rx < 0 || ry < 0) {
    if (rx >= 0) {
      return rx == y;
    }
    if (ry >= 0) {
      return ry == x;
    }
    return false;
  }
  return rx == ry;
}

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

  init();

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

  vb ck(n, false);

  rep(i, 0, n) ck[root(i)] = true;

  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

#include <iostream>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> data;
  int ngroup;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  bool isRoot(int x) { return data[x] < 0; }
  int size(int x) { return -data[root(x)]; }
};

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.unionSet(a, b);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (uf.isRoot(i)) {
      ans++;
    }
  }
  ans--;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> data;
  int ngroup;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

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.unionSet(a, b);
  }

  int ans = n;
  for (int i = 0; i < n; i++) {
    if (uf.root(i) == i) {
      ans--;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <algorithm> //next_permutation
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const ll MOD = 1000000007;
const ll MAX = 2000001;

ll mod(ll a) { return a % MOD; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

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

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

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

  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;

    UF.unite(x, y);
  }

  set<ll> parent;

  rep(i, n) { parent.insert(UF.root(i)); }

  cout << parent.size() - 1 << endl;
}


Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <algorithm> //next_permutation
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const ll MOD = 1000000007;
const ll MAX = 2000001;

ll mod(ll a) { return a % MOD; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

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

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

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

  rep(i, m) {
    ll x, y;
    cin >> x >> y;

    UF.unite(x, y);
  }

  set<ll> parent;

  rep(i, n) { parent.insert(UF.root(i)); }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  priority_queue<int> q;
  vector<int> a;
  vector<int> vis(n, 0);
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      q.push(i);
      vis[i] = 1;
      while (!q.empty()) {
        int x = q.top();
        q.pop();
        a.push_back(x);
        for (auto j : g[x])
          if (vis[j] == 0) {
            vis[j] = 1;
            q.push(j);
          }
      }
      a.push_back(-1);
    }
  }
  int ans = 0;
  for (auto i : a)
    if (i == -1)
      ans++;
  cout << max(ans - 1, 0) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<vector<int>> g;
vector<int> a;
int ans;
void dfs(int x, int prev) {
  for (auto i : g[x])
    if (i != prev) {
      ans++;
      dfs(i, x);
    }
}
int main() {
  int n, m;
  cin >> n >> m;
  g.resize(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(0, -1);
  cout << ans << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// UnionFind
class UnionFind {
public:
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    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;
    --a;
    --b;
    uf.unite(a, b);
  }
  int ans = n;
  int k = 0;
  rep(i, n) {
    if (uf.d[i] < 0) {
      ans += uf.d[i];
      ++k;
    }
  }
  --ans;
  ++k;
  ans += k - 1;
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// UnionFind
class UnionFind {
  vector<int> d;

public:
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    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;
    --a;
    --b;
    uf.unite(a, b);
  }
  int ans = n - uf.size(n);
  cout << ans << endl;

  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <atcoder/dsu>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define rREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define rFOR(i, a, b) for (int i = a; i >= b; --i)
#define aFOR(i, V) for (auto i : V)
#define ALL(vec) vec.begin(), vec.end()
#define rALL(vec) vec.rbegin(), vec.rend()
#define INF 1'000'000'007              // 10^9 + 7
#define MOD 1'000'000'007              // 10^9 + 7
#define lINF 1'000'000'000'000'000'007 // 10^18 + 7
// ------------------------------

vector<int> group;
int num = 0;

void bfs(vector<vector<int>> vec, int now) {
  ++num;
  group[now] = num;
  queue<int> q;
  q.push(now);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    group[now] = num;
    aFOR(i, vec[now]) {
      if (!group[i]) {
        q.push(i);
        group[i] = num;
      }
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> vec(N, vector<int>(0));
  vector<int> tmp(N, 0);
  group = tmp;
  dsu d(N);
  REP(i, M) {
    int A, B;
    cin >> A >> B;
    --A, --B;
    d.merge(A, B);
  }
  set<int> s;
  REP(i, N) { s.insert(d.leader(i)); }
  cout << s.size() - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define rREP(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define rFOR(i, a, b) for (int i = a; i >= b; --i)
#define aFOR(i, V) for (auto i : V)
#define ALL(vec) vec.begin(), vec.end()
#define rALL(vec) vec.rbegin(), vec.rend()
#define INF 1'000'000'007              // 10^9 + 7
#define MOD 1'000'000'007              // 10^9 + 7
#define lINF 1'000'000'000'000'000'007 // 10^18 + 7
// ------------------------------

vector<int> group;
int num = 0;

void bfs(vector<vector<int>> vec, int now) {
  ++num;
  group[now] = num;
  queue<int> q;
  q.push(now);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    group[now] = num;
    aFOR(i, vec[now]) {
      if (!group[i]) {
        q.push(i);
        group[i] = num;
      }
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> vec(N, vector<int>(0));
  vector<int> tmp(N, 0);
  group = tmp;
  REP(i, M) {
    int A, B;
    cin >> A >> B;
    --A, --B;
    vec[A].push_back(B);
    vec[B].push_back(A);
  }
  int flag = true, now = 0;
  REP(i, N) if (!group[i]) {
    now = i;
    flag = true;
    bfs(vec, now);
  }
  cout << num - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, M;
  cin >> N >> M;
  graph r(N + 1);
  rep(i, M) {
    int A, B;
    cin >> A >> B;

    r[A].push_back(B);

    r[B].push_back(A);
  }
  vector<bool> rooted(N + 1, false);
  int ans = 0;
  REP(i, N) {
    if (rooted[i] == false) {
      rooted[i] = true;
      queue<int> q;
      for (int &e : r[i]) {
        q.push(e);
      }
      while (!q.empty()) {
        int a = q.front();
        rooted[a] = true;
        q.pop();
        for (int &e : r[a]) {
          if (rooted[e] == false)
            q.push(e);
        }
      }
      ans++;
    }
  }

  cout << ans - 1 << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, M;
  cin >> N >> M;
  graph r(N + 1);
  rep(i, M) {
    int A, B;
    cin >> A >> B;

    r[A].push_back(B);

    r[B].push_back(A);
  }
  vector<bool> rooted(N + 1, false);
  int ans = 0;
  REP(i, N) {
    if (rooted[i] == false) {
      rooted[i] = true;
      queue<int> q;
      for (int &e : r[i]) {
        q.push(e);
      }
      while (!q.empty()) {
        int a = q.front();
        rooted[a] = true;
        q.pop();
        for (int &e : r[a]) {
          q.push(e);
        }
      }
      ans++;
    }
  }

  cout << ans - 1 << endl;
}