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

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

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

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

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }

  int cnt() {
    int cnt = 0;
    for (auto it : data)
      if (it < 0)
        cnt++;
    return cnt;
  }
};

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);
  }
  cout << uf.cnt() - 1 << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Error: 0

Fixed submission:

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

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

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

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }

  int cnt() {
    int cnt = 0;
    for (auto it : data)
      if (it < 0)
        cnt++;
    return cnt;
  }
};

void solve() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }
  cout << uf.cnt() - 1 << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(v) v.begin(), v.end()
#define ve vector
#define vi vector<int>
#define vi2 vector<vi>
#define vl vector<long long>
#define vl2 vector<vl>

template <class T> T maxAny(T a, T b) {
  if (a > b)
    return a;
  else
    return b;
}

template <class T> T minAny(T a, T b) {
  if (a > b)
    return b;
  else
    return a;
}

vector<int> mapping(100000);

int search(int a) {
  queue<int> q;
  int nexta = a;
  while (mapping[nexta] != nexta) {
    q.push(nexta);
    nexta = mapping[nexta];
  }
  while (!q.empty()) {
    mapping[q.front()] = nexta;
    q.pop();
  }
  return nexta;
}

int main() {
  int n, m;
  cin >> n >> m;
  int a[m], b[m];
  mapping.resize(n + 1);
  rep(i, m) cin >> a[i] >> b[i];
  mapping[0] = -1;
  rep1(i, n) { mapping[i] = i; }
  rep(i, m) {
    int a_moto = search(a[i]), b_moto = search(b[i]);
    if (mapping[a_moto] != mapping[b_moto])
      mapping[b_moto] = a_moto;
  }
  int ans = -1;
  rep1(i, n) {
    if (mapping[i] == i)
      ans++;
  }
  cout << ans << endl;
}
/*
int main(){
    int n, k;
    cin >> n >> k;
    int a[n];
    rep(i, n) cin >> a[i];

}*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(v) v.begin(), v.end()
#define ve vector
#define vi vector<int>
#define vi2 vector<vi>
#define vl vector<long long>
#define vl2 vector<vl>

template <class T> T maxAny(T a, T b) {
  if (a > b)
    return a;
  else
    return b;
}

template <class T> T minAny(T a, T b) {
  if (a > b)
    return b;
  else
    return a;
}

vector<int> mapping(100000);

int search(int a) {
  queue<int> q;
  int nexta = a;
  while (mapping[nexta] != nexta) {
    q.push(nexta);
    nexta = mapping[a];
  }
  while (!q.empty()) {
    mapping[q.front()] = nexta;
    q.pop();
  }
  return nexta;
}

int main() {
  int n, m;
  cin >> n >> m;
  int a[m], b[m];
  mapping.resize(n + 1);
  rep(i, m) cin >> a[i] >> b[i];
  mapping[0] = -1;
  rep1(i, n) { mapping[i] = i; }
  rep(i, m) {
    int a_moto = search(a[i]), b_moto = search(b[i]);
    if (mapping[a_moto] != mapping[b_moto])
      mapping[b_moto] = a_moto;
  }
  int ans = -1;
  rep1(i, n) {
    if (mapping[i] == i)
      ans++;
  }
  cout << ans << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
void dfs(int u, vector<vector<int>> &graph, vector<bool> &visited) {
  visited[u] = true;
  for (auto v : graph[u]) {
    if (!visited[v])
      dfs(v, graph, visited);
  }
}
int main() {

  int n, m;
  cin >> n >> m;
  vector<bool> visited(n + 1, false);
  vector<vector<int>> graph(n + 1);
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  int count = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      count++;
      dfs(i, graph, visited);
    }
  }
  cout << count - 1 << '\\n';
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
void dfs(int u, vector<vector<int>> &graph, int visited[], int count) {
  visited[u] = count;
  for (auto v : graph[u]) {
    if (!visited[v])
      dfs(v, graph, visited, count);
  }
}
int main() {

  int n, m;
  cin >> n >> m;
  int visited[n + 1];
  vector<vector<int>> graph(n + 1);
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  int count = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i, graph, visited, count);
      count++;
    }
  }
  cout << count << '\\n';
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
queue<int> q;
vector<int> edges[100005];
bool visited[100005];
int n, m;
int ans = 0;
void bfs(int x) {
  int ret = 0;
  visited[x] = 1;
  q.push(x);
  while (!q.empty()) {
    int nx = q.front();
    q.pop();
    for (int i = 0; i < edges[nx].size(); i++) {
      int y = edges[nx][i];
      if (!visited[y]) {
        visited[y] = 1;
        q.push(y);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edges[a].push_back(b), edges[b].push_back(a);
  }
  for (int i = 0; i < n; i++)
    if (!visited[i]) {
      ans++;
      bfs(i);
    }
  cout << ans - 1;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) a.size()
#define re return
#define MP make_pair
#define pb push_back
#define pii pair<int, int>
#define se second
#define fi first
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
queue<int> q;
vector<int> edges[100005];
bool visited[100005];
int n, m;
int ans = 0;
void bfs(int x) {
  int ret = 0;
  visited[x] = 1;
  q.push(x);
  while (!q.empty()) {
    int nx = q.front();
    q.pop();
    rep(i, 0, sz(edges[nx]) - 1) {
      int y = edges[nx][i];
      if (!visited[y]) {
        visited[y] = 1;
        q.push(y);
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edges[a].pb(b), edges[b].pb(a);
  }
  for (int i = 0; i < n; i++)
    if (!visited[i]) {
      ans++;
      bfs(i);
    }
  cout << ans - 1;
  re 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = pair<ii, int>;
#define vt vector
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int N = 1e5 + 5;

int n, m, a, b, ans;
int par[N];
bool vis[N];

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

int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }

void join(int x, int y) { par[root(x)] = root(y); }

void solve() {
  int T = 1;
  // cin >> T;
  for (int tc = 1; tc <= T; tc++) {
    cin >> n >> m;
    init();
    while (m--) {
      cin >> a >> b;
      join(a, b);
    }
    for (int i = 1; i <= n; i++) {
      if (!vis[root(i)]) {
        vis[root(i)] = true;
        ans++;
      }
    }
    cout << ans - 1 << "\\n";
  }
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = pair<ii, int>;
#define vt vector
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int N = 1e5 + 5;

int n, m, a, b, ans;
int par[N];
bool vis[N];

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

int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }

void join(int x, int y) { par[root(x)] = y; }

void solve() {
  int T = 1;
  // cin >> T;
  for (int tc = 1; tc <= T; tc++) {
    cin >> n >> m;
    init();
    while (m--) {
      cin >> a >> b;
      join(a, b);
    }
    for (int i = 1; i <= n; i++) {
      if (!vis[root(i)]) {
        vis[root(i)] = true;
        ans++;
      }
    }
    cout << ans - 1 << "\\n";
  }
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

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

#include <iostream>
#include <vector>

using namespace std;

int P(int i, vector<int> &p) {
  if (p[i] == i)
    return i;
  else
    return p[i] = P(p[i], p);
}

void C(int a, int b, vector<int> &p) { p[P(a, p)] = P(b, p); }

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> p(N);
  int i;
  for (i = 0; i < N; i++)
    p[i] = i;
  int a, b;
  for (i = 0; i < M; i++) {
    cin >> a >> b;
    C(a - 1, b - 1, p);
  }
  vector<bool> f(N, false);
  int cnt = 0;
  for (i = 0; i < N; i++) {
    if (!f[P(i, p)]) {
      f[p[i]] = true;
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int P(int i, vector<int> &p) {
  if (p[i] == i)
    return i;
  else
    return p[i] = P(p[i], p);
}

void C(int a, int b, vector<int> &p) { p[P(a, p)] = P(b, p); }

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> p(N);
  int i;
  for (i = 0; i < N; i++)
    p[i] = i;
  int a, b;
  for (i = 0; i < M; i++) {
    cin >> a >> b;
    C(a, b, p);
  }
  vector<bool> f(N, false);
  int cnt = 0;
  for (i = 0; i < N; i++) {
    if (!f[P(i, p)]) {
      f[p[i]] = true;
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int p[100010], ans, a, b;
int n, m;
int fifa(int x) {
  if (p[x] == x)
    return x;
  int root = fifa(p[x]);
  return p[x] = root;
}
void join(int x, int y) {
  int px = fifa(x);
  int py = fifa(y);
  if (px != py) {
    p[px] = py;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    p[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    join(x, y);
  }
  for (int i = 1; i <= n; i++) {
    if (fifa(i) == i) {
      ans++;
    }
  }
  printf("%d", ans - 1);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int p[100010], ans, a[100010], b[100010], son[100010];
int n, m;
int fifa(int x) {
  int t = x;
  while (p[t] != t) {
    t = p[t];
  }
  while (p[x] != t) {
    int tmp = p[x];
    p[x] = t;
    x = tmp;
  }
}
bool join(int x, int y) {
  int px = fifa(x);
  int py = fifa(y);
  if (px != py) {
    p[px] = py;
    son[py] += son[px];
    if (son[py] == n) {
      return true;
    }
    return false;
  }
  return false;
}
bool query(int x, int y) { return fifa(x) == fifa(y); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    son[i] = 1;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &a[i], &b[i]);
  }
  for (int i = 1; i <= m; i++) {
    int x = fifa(a[i]), y = fifa(b[i]);
    if (!join(x, y)) {
      ans++;
    }
  }
  printf("%d", ans);
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(begin, end) for (i = begin; i < end; i++)
#define repj(begin, end) for (j = begin; j < end; j++)
#define init(arr, end, val)                                                    \\
  for (i = 0; i < end; i++)                                                    \\
    arr[i] = val;
#define printint(i0, i1) printf("%d %d\\n", i0, i1)

using namespace std;
typedef long long int ll;
const int inf = 1000000000;
const int mod = 1000000007;
const int nil = -1;
int i, n, m, k, a, b;
bool checked[100001];
queue<int> q;
set<pair<int, int>> s;

int main() {
  scanf(" %d %d", &n, &m);
  rep(1, n + 1) checked[i] = false;
  rep(0, m) {
    scanf(" %d %d", &a, &b);
    s.insert(make_pair(a, b));
    s.insert(make_pair(b, a));
  }
  int cnt = 0;
  rep(1, n + 1) {
    if (!checked[i]) {
      cnt++;
      q.push(i);
      while (q.size() != 0) {
        int cur = q.front();
        q.pop();
        checked[cur] = true;
        for (auto it = s.lower_bound(make_pair(cur, -1));
             it != s.lower_bound(make_pair(cur + 1, -1)); it++) {
          if (!checked[it->second])
            q.push(it->second);
        }
      }
    }
  }
  printf("%d\\n", cnt - 1);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(begin, end) for (i = begin; i < end; i++)
#define repj(begin, end) for (j = begin; j < end; j++)
#define init(arr, end, val)                                                    \\
  for (i = 0; i < end; i++)                                                    \\
    arr[i] = val;
#define printint(i0, i1) printf("%d %d\\n", i0, i1)

using namespace std;
typedef long long int ll;
const int inf = 1000000000;
const int mod = 1000000007;
const int nil = -1;
int i, n, m, k, a, b;
vector<int> v[100000];
bool checked[100000];
queue<int> q;

void bfs(int num) {}

int main() {
  scanf(" %d %d", &m, &n);
  rep(0, n) {
    scanf(" %d %d", &a, &b);
    v[a].push_back(b);
    v[b].push_back(a);
    checked[i] = false;
  }
  int cnt = 0;
  rep(0, n) {
    if (!checked[i]) {
      cnt++;
      q.push(i);
      while (q.size() != 0) {
        int cur = q.front();
        q.pop();
        checked[cur] = true;
        for (auto it = v[cur].begin(); it != v[cur].end(); it++) {
          if (!checked[*it])
            q.push(*it);
        }
      }
    }
  }
  printf("%d\\n", cnt);
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <vector>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
#include <string>

using namespace std;
typedef long long ll;

#define rep(i, k, n) for (ll i = k; i < (ll)(n); i++)

struct UnionFind {
  vector<ll> par; // par[a]=b: aの親がb

  UnionFind(ll N) : par(N) { // 初めは全て根として初期化
    rep(i, 0, N) par[i] = i;
  }

  ll root(ll x) { // xの属する根を求める
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(ll x, ll y) { // xとyの木を併合
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return; // 根が同じときはそのまま
    par[rx] = ry;
  }

  bool same(ll x, ll y) { // x,y の属する木が同じならtrue
    return root(x) == root(y);
  }
};

int main() {
  ll n, m;
  cin >> n >> m;
  UnionFind tree(n);
  vector<ll> a(m), b(m);
  rep(i, 0, m) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
    tree.unite(a[i], b[i]);
  }

  ll ans = 0;
  vector<ll> group_size(n);
  rep(i, 0, n) { // 各グループの人数を数える
    group_size[tree.root(i)]++;
  }
  rep(i, 0, n) {
    if (group_size[i] > 0)
      ans++;
  }

  cout << ans - 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <vector>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
#include <string>

using namespace std;
typedef long long ll;

#define rep(i, k, n) for (ll i = k; i < (ll)(n); i++)

struct UnionFind {
  vector<ll> par; // par[a]=b: aの親がb

  UnionFind(ll N) : par(N) { // 初めは全て根として初期化
    rep(i, 0, N) par[i] = i;
  }

  ll root(ll x) { // xの属する根を求める
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(ll x, ll y) { // xとyの木を併合
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return; // 根が同じときはそのまま
    par[rx] = ry;
  }

  bool same(ll x, ll y) { // x,y の属する木が同じならtrue
    return root(x) == root(y);
  }
};

int main() {
  ll n, m;
  cin >> n >> m;
  UnionFind tree(n);
  vector<ll> a(m), b(m);
  rep(i, 0, m) {
    cin >> a[i] >> b[i];
    tree.unite(a[i] - 1, b[i] - 1);
  }

  ll ans = 0;
  rep(i, 0, n - 1) {
    rep(j, i + 1, n) {
      if (tree.same(i, j)) {
        // cout << "Yes" << endl;
      } else {
        // cout << "No" << endl;
        tree.unite(i, j);
        ans++;
      }
    }
  }

  cout << ans << endl;

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

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

#define ll long long
const int N = 2e5 + 9;

vector<int> adj[N];
int n, m;
bool vis[N];
void dfs(int u) {
  vis[u] = 1;
  for (auto v : adj[u]) {
    if (vis[v] == 1)
      continue;
    dfs(v);
  }
}

int main() {

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      dfs(i);
      ans++;
    }
  }
  cout << max(0LL, ans - 1) << endl;
}


Error: 0

Fixed submission:

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

#define ll long long
const int N = 2e5 + 9;

vector<int> adj[N];
int n, m;
bool vis[N];
void dfs(int u, int par) {
  vis[u] = 1;
  for (auto v : adj[u]) {
    if (v == par)
      continue;
    dfs(v, u);
  }
}

int main() {

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      dfs(i, 0);
      ans++;
    }
  }
  cout << max(0LL, ans - 1) << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

// In the name of god
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define use_file                                                               \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
using namespace std;
using ll = long long;
const ll N = 1e5 + 5, inf = 4e18, mod = 1e9 + 7;
ll n, m, ans;
vector<ll> adj[N];
bool mark[N];
void dfs(ll u) {
  mark[u] = 1;
  for (int j : adj[u]) {
    if (!mark[j])
      dfs(j);
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1;
  cout << endl;
}


Error: 0

Fixed submission:

// In the name of god
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define use_file                                                               \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
using namespace std;
using ll = long long;
const ll N = 1e5, inf = 4e18, mod = 1e9 + 7;
ll n, m, ans;
vector<ll> adj[N];
bool mark[N];
void dfs(ll u) {
  mark[u] = 1;
  for (int j : adj[u]) {
    if (!mark[j])
      dfs(j);
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1;
  cout << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define read(a) scanf("%d", &a)
#define readf(a) scanf("%lf", &a)
#define pb push_back
#define mk make_pair
#define _rand mt19937 RAND(time(0))
#define mem(a) memset(a, 0, sizeof(a))
#define Buff ios::sync_with_stdio(false)
typedef long long ll;
using namespace std;
const int INF = 1e9 + 7;
const int N = 3e5 + 7;
const int M = 1e6 + 7;
int fa[N];

int get(int x) { return fa[x] = fa[x] == x ? x : get(fa[x]); }
signed main() {
  Buff;
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  while (m--) {
    int u, v;
    cin >> u >> v;
    u = get(u);
    v = get(v);
    fa[u] = v;
  }

  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (i == get(i))
      cnt++;

  cout << cnt - 1 << "\\n";
  return 0;
}
/**
 **/


Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define read(a) scanf("%d", &a)
#define readf(a) scanf("%lf", &a)
#define pb push_back
#define mk make_pair
#define _rand mt19937 RAND(time(0))
#define mem(a) memset(a, 0, sizeof(a))
#define Buff ios::sync_with_stdio(false)
typedef long long ll;
using namespace std;
const int INF = 1e9 + 7;
const int N = 3e5 + 7;
const int M = 1e6 + 7;

#define lson rt << 1
#define rson rt << 1 | 1
template <class T> class SegTree {
public:
  T maxv[N << 2];
  void init() { mem(maxv); }
  inline void pushup(int rt) { maxv[rt] = max(maxv[lson], maxv[rson]); }
  void update(int rt, int l, int r, int pos, T val) {
    if (l == r) {
      maxv[rt] = max(maxv[rt], val);
      return;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid)
      update(lson, l, mid, pos, val);
    else
      update(rson, mid + 1, r, pos, val);
    pushup(rt);
  }

  T query(int rt, int l, int r, int L, int R) {
    if (L <= l && r <= R)
      return maxv[rt];
    int mid = (l + r) >> 1;
    T ans = 0;
    if (L <= mid)
      ans = max(ans, query(lson, l, mid, L, R));
    if (R > mid)
      ans = max(ans, query(rson, mid + 1, r, L, R));
    return ans;
  }
};
SegTree<int> tree;

int fa[N];

int get(int x) { return fa[x] == x ? x : get(fa[x]); }
signed main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  while (m--) {
    int u, v;
    cin >> u >> v;
    u = get(u);
    v = get(v);
    fa[u] = v;
  }

  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (i == get(i))
      cnt++;

  cout << cnt - 1 << "\\n";
  return 0;
}
/**
 **/