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

#include <iostream>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
  int groups;

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
    groups = N;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    groups--;
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind t(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    t.unite(a, b);
  }
  cout << t.groups - 1 << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
  int groups;

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
    groups = N;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    groups--;
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind t(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    t.unite(a, b);
  }
  cout << t.groups - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind tree(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    tree.unite(a - 1, b - 1);
  }
  int cnt = 0;
  rep(i, n) {
    if (tree.par[i] == i)
      cnt++;
  }
  cout << cnt - 1 << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind tree(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    tree.unite(a, b);
  }
  int cnt = 0;
  rep(i, n) {
    if (tree.par[i] == i)
      cnt++;
  }
  cout << cnt - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;

struct UnionFind {
  vector<int> d;
  // n要素で初期化
  UnionFind(int n) : d(n, -1) {}
  // 木の根を決める
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  // xとy属する集合を併合
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  // xとyが同じ集合に属するか否か
  bool same(int x, int y) { return root(x) == root(y); }
  // xの属する集合のサイズ
  int size(int x) { return -d[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.unite(a, b);
  }

  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (!uf.same(i, i + 1)) {
      uf.unite(i, i + 1);
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;

struct UnionFind {
  vector<int> d;
  // n要素で初期化
  UnionFind(int n) : d(n, -1) {}
  // 木の根を決める
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  // xとy属する集合を併合
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  // xとyが同じ集合に属するか否か
  bool same(int x, int y) { return root(x) == root(y); }
  // xの属する集合のサイズ
  int size(int x) { return -d[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.unite(a, b);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!uf.same(i, i + 1)) {
      uf.unite(i, i + 1);
      ans++;
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;

long long modpow(long long n, long long k, long long mod) {
  if (k == 0)
    return 1;
  long long r = modpow(n * n % mod, k >> 1, mod);
  if (k & 1)
    r = r * n % mod;
  return r;
}
vector<vector<int>> adj(1e5 + 10);
vector<bool> b(1e5 + 10, 0);
void dfs(int v) {
  b[v] = 1;
  for (int i = 0; i < adj[v].size(); i++) {
    int vi = adj[v][i];
    if (!b[vi])
      dfs(vi);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  int ans = -1;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    v--;
    u--;
    adj[v].push_back(u);
    adj[u].push_back(v);
  }
  for (int i = 0; i < n; i++) {
    if (!b[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans;
}

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

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;

long long modpow(long long n, long long k, long long mod) {
  if (k == 0)
    return 1;
  long long r = modpow(n * n % mod, k >> 1, mod);
  if (k & 1)
    r = r * n % mod;
  return r;
}
vector<vector<int>> adj(1e5 + 10);
vector<bool> b(1e5 + 10, 0);
void dfs(int v, int prev) {
  b[v] = 1;
  for (int i = 0; i < adj[v].size(); i++) {
    int vi = adj[v][i];
    if (vi == prev)
      continue;
    dfs(vi, v);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  int ans = -1;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    v--;
    u--;
    adj[v].push_back(u);
    adj[u].push_back(v);
  }
  for (int i = 0; i < n; i++) {
    if (!b[i]) {
      dfs(i, -1);
      ans++;
    }
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

void dfs(int u, vector<vector<int>> &g, vector<bool> &vis) {

  vis[u] = 1;
  for (auto v : g[u]) {

    if (vis[v] == 0) {
      dfs(v, g, vis);
    }
  }
}

int main() {

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

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

  for (int i = 0; i < m; i++) {

    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 1; i <= n; i++) {

    if (vis[i] == false) {

      dfs(i, g, vis);
      count++;
    }
  }
  cout << count - 1 << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int n;
vector<vector<int>> g(n);
vector<bool> vis(n, false);

void dfs(int u) {

  vis[u] = true;
  for (auto v : g[u]) {
    if (vis[v])
      continue;

    vis[v] = true;
    dfs(v);
  }
}

int main() {

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

  for (int i = 0; i < m; i++) {

    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 0; i < n; i++) {

    if (vis[i] == true)
      continue;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 999999999
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
ll cnt = 0, ans = 0;
const int MAX = 510000;

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
int main() {

  ll n, m;
  cin >> n >> m;
  UnionFind tree(n + 1);

  vector<int> a(m);
  vector<int> b(m);
  for (int i = 0; i < m; i++) {
    cin >> a.at(i) >> b.at(i);
    int c, d;
    c = a.at(i);
    d = b.at(i);
    tree.unite(c, d);
  }

  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    mp[tree.root(i)]++; // 根を数える
  }

  for (int i = 0; i <= n; i++) {

    if (mp[i] != 0)
      ans++;
  }
  if (ans == 1) {
    cout << 0 << endl;
    return 0;
  }
  cout << ans - 1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 999999999
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
ll cnt = 0, ans = 0;
const int MAX = 510000;

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
int main() {

  ll n, m;
  cin >> n >> m;
  UnionFind tree(n);
  vector<int> a(m);
  vector<int> b(m);
  for (int i = 0; i < m; i++) {
    cin >> a.at(i) >> b.at(i);
    int c, d;
    c = a.at(i);
    d = b.at(i);
    tree.unite(c, d);
  }

  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    mp[tree.root(i)]++; // 根を数える
  }

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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rt return 0
#define endln "\\n"
#define all(v) v.begin(), v.end()
#define for1(i, a, b) for (long long int i = a; i < b; i++)
#define for2(i, a, b) for (long long int i = a; i > b; i--)
//  cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;typedef long long
//  int i;
typedef long long int lli;
typedef pair<lli, lli> pll;
int v, E;
vector<vector<int>> g; // adjacency list
vector<int> vis;       // stores visit status
void dfs(int cur) {
  // int p = cur;
  for (int j = 0; j < g[cur].size(); j++) {
    int k = g[cur][j];
    if (vis[k] == 0) {
      vis[k] = 1;
      dfs(k);
    }
  }
  // cout << cur << " ";
  // return 0;
}
int call_dfs() {
  int x = 0;
  for (int i = 1; i <= v; i++) {
    if (vis[i] == 0) {
      vis[i] = 1;
      dfs(i);
      x += 1;
    }
  }
  return x - 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // lli n, m;
  cin >> v >> E;
  g.resize(v + 1);
  vis.resize(v + 1);
  for (int i = 1; i <= v; i++) {
    vis[i] = 0;
  }
  for (int i = 0; i < E; i++) {
    int v1, v2;
    cin >> v1 >> v2;
    g[v1].push_back(v2);
    g[v2].push_back(v1);
  }
  cout << call_dfs() << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rt return 0
#define endln "\\n"
#define all(v) v.begin(), v.end()
#define for1(i, a, b) for (long long int i = a; i < b; i++)
#define for2(i, a, b) for (long long int i = a; i > b; i--)
//  cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;typedef long long
//  int i;
typedef long long int lli;
typedef pair<lli, lli> pll;
int v, E, x = 0;
vector<vector<int>> g; // adjacency list
vector<int> vis;       // stores visit status
int dfs(int cur) {
  int p = cur;
  for (int j = 0; j < g[cur].size(); j++) {
    int k = g[cur][j];
    if (vis[k] == 0) {
      vis[k] = 1;
      dfs(k);
    }
  }
  // cout << cur << " ";
  return 0;
}
int call_dfs() {
  for (int i = 1; i <= v; i++) {
    if (vis[i] == 0) {
      vis[i] = 1;
      dfs(i);
      x += 1;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // lli n, m;
  cin >> v >> E;
  g.resize(v + 1);
  vis.resize(v + 1);
  for (int i = 1; i <= v; i++) {
    vis[i] = 0;
  }
  for (int i = 0; i < E; i++) {
    int v1, v2;
    cin >> v1 >> v2;
    g[v1].push_back(v2);
    g[v2].push_back(v1);
  }
  call_dfs();
  cout << x - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <atcoder/dsu>
#include <cstdio>
#include <iostream>
#include <set>
#include <stack>
#include <vector>
using namespace std;
using namespace atcoder;
#define N 100000

int main(void) {
  int n, m;
  cin >> n >> m;
  dsu g(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g.merge(a, b);
  }
  set<int> ans;
  for (int i = 1; i <= n; i++) {
    int k = g.leader(i);
    ans.insert(k);
  }
  cout << ans.size() - 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define N 100000

int main(void) {
  int n, m, neigh, ver;
  cin >> n >> m;
  vector<int> G[N];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> visit(n, 0);
  int chain[N];
  stack<int> S;
  for (int i = 1; i <= n; i++) {
    if (visit[i] == 0) {
      S.push(i);
      while (!S.empty()) {
        ver = S.top();
        S.pop();
        visit[ver] = 1;
        chain[ver] = i;
        for (int j = 0; j < (int)G[ver].size(); j++) {
          neigh = G[ver][j];
          if (visit[neigh] == 0)
            S.push(neigh);
        }
      }
    }
  }
  set<int> ans;
  for (int i = 1; i <= n; i++) {
    ans.insert(chain[i]);
  }
  cout << ans.size() - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> parent;
  int N;
  UnionFind(int n) : parent(n), N(n) { // メンバイニシャライザを実行している
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
  }
  int getParent(int x) { return parent[x]; }

  int root(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      parent[x] = root(parent[x]); // 親の付け替え(ならし)
      return parent[x];
    }
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx != ry) {
      parent[ry] = rx;
    }
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(int argc, char *argv[]) {
  int N, M;
  scanf("%d %d", &N, &M);
  vector<int> A(M);
  vector<int> B(M);
  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", &A[i], &B[i]);
    if (A[i] > B[i]) {
      int tmp;
      tmp = B[i];
      B[i] = A[i];
      A[i] = tmp;
    }
    tree.unite(A[i] - 1, B[i] - 1);
  }
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (tree.getParent(i) == i) {
      count++;
    }
  }
  printf("%d\\n", count - 1);
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> parent;
  int N;
  UnionFind(int n) : N(n) { // メンバイニシャライザを実行している
    for (int i = 0; i < N; i++) {
      parent[i] = i;
    }
  }
  int getParent(int x) { return parent[x]; }

  int root(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      parent[x] = root(parent[x]); // 親の付け替え(ならし)
      return parent[x];
    }
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx != ry) {
      parent[ry] = rx;
    }
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(int argc, char *argv[]) {
  int N, M;
  scanf("%d %d", &N, &M);
  vector<int> A(M);
  vector<int> B(M);
  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", &A[i], &B[i]);
    if (A[i] > B[i]) {
      int tmp;
      tmp = B[i];
      B[i] = A[i];
      A[i] = tmp;
    }
    tree.unite(A[i] - 1, B[i] - 1);
  }
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (tree.getParent(i) == i) {
      count++;
    }
  }
  printf("%d\\n", count - 1);
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> parent;
  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = i;
    }
  }
  int getParent(int x) { return parent[x]; }

  int root(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      parent[x] = root(parent[x]);
      return parent[x];
    }
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx != ry) {
      parent[ry] = rx;
    }
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(int argc, char *argv[]) {
  int N, M;
  scanf("%d %d", &N, &M);
  vector<int> A(M);
  vector<int> B(M);
  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", &A[i], &B[i]);
    if (A[i] > B[i]) {
      int tmp;
      tmp = B[i];
      B[i] = A[i];
      A[i] = tmp;
    }
    tree.unite(A[i] - 1, B[i] - 1);
  }
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (tree.getParent(i) == i) {
      count++;
    }
  }
  printf("%d\\n", count - 1);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> parent;
  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = i;
    }
  }
  int getParent(int x) { return parent[x]; }

  int root(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return root(parent[x]);
    }
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx != ry) {
      parent[ry] = rx;
    }
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(int argc, char *argv[]) {
  int N, M;
  scanf("%d %d", &N, &M);
  vector<int> A(M);
  vector<int> B(M);
  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", &A[i], &B[i]);
    if (A[i] > B[i]) {
      int tmp;
      tmp = B[i];
      B[i] = A[i];
      A[i] = tmp;
    }
    tree.unite(A[i] - 1, B[i] - 1);
  }
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (tree.getParent(i) == i) {
      count++;
    }
  }
  printf("%d\\n", count - 1);
}