Problem p03682 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define ALL(c) (c).begin(), (c).end()
const int MOD = 1000000007;

class UnionFind {
  vector<int> parent;

public:
  UnionFind(int size) { parent = vector<int>(size, -1); }

  int find(int x) {
    if (parent[x] < 0) {
      // parent itself
      return x;
    } else {
      return parent[x] = find(parent[x]);
    }
  }

  bool unite(int x, int y) {
    int rX, rY;
    if ((rX = find(x)) == (rY = find(y))) {
      // already united
      return false;
    }
    if (size(rX) < size(rY)) {
      int tmp = rX;
      rX = rY;
      rY = tmp;
    }
    parent[rX] += parent[rY];
    parent[rY] = rX;
    return true;
  }

  int size(int x) { return -parent[find(x)]; }

  bool isSame(int x, int y) { return find(x) == find(y); }

  int component_count() {
    int c = 0;
    for (const int x : parent) {
      if (x < 0) {
        c++;
      }
    }
    return c;
  }
};

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

  vector<pair<int, int>> x, y;
  int r, c;
  for (int i = 0; i < n; i++) {
    cin >> r >> c;
    x.emplace_back(r, i);
    y.emplace_back(c, i);
  }

  sort(ALL(x));
  sort(ALL(y));
  // dist, idx1, idx2
  vector<tuple<int, int, int>> d;
  for (int i = 1; i < n; i++) {
    d.emplace_back(abs(x[i].first - x[i - 1].first), x[i].second,
                   x[i - 1].second);
    d.emplace_back(abs(y[i].first - y[i - 1].first), y[i].second,
                   y[i - 1].second);
  }

  sort(ALL(d));

  UnionFind uf = UnionFind(n);

  int t = 0;
  for (const auto e : d) {
    if (uf.component_count() == 1) {
      break;
    }

    if (uf.isSame(get<1>(e), get<2>(e))) {
      continue;
    }

    t += get<0>(e);
    uf.unite(get<1>(e), get<2>(e));
  }

  cout << t << endl;
}


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

using ll = long long;
#define ALL(c) (c).begin(), (c).end()
const int MOD = 1000000007;

class UnionFind {
  vector<int> parent;

public:
  UnionFind(int size) { parent = vector<int>(size, -1); }

  int find(int x) {
    if (parent[x] < 0) {
      // parent itself
      return x;
    } else {
      return parent[x] = find(parent[x]);
    }
  }

  bool unite(int x, int y) {
    int rX, rY;
    if ((rX = find(x)) == (rY = find(y))) {
      // already united
      return false;
    }
    if (size(rX) < size(rY)) {
      int tmp = rX;
      rX = rY;
      rY = tmp;
    }
    parent[rX] += parent[rY];
    parent[rY] = rX;
    return true;
  }

  int size(int x) { return -parent[find(x)]; }

  bool isSame(int x, int y) { return find(x) == find(y); }

  int component_count() {
    int c = 0;
    for (const int x : parent) {
      if (x < 0) {
        c++;
      }
    }
    return c;
  }
};

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

  vector<pair<int, int>> x, y;
  int r, c;
  for (int i = 0; i < n; i++) {
    cin >> r >> c;
    x.emplace_back(r, i);
    y.emplace_back(c, i);
  }

  sort(ALL(x));
  sort(ALL(y));
  // dist, idx1, idx2
  vector<tuple<int, int, int>> d;
  for (int i = 1; i < n; i++) {
    d.emplace_back(abs(x[i].first - x[i - 1].first), x[i].second,
                   x[i - 1].second);
    d.emplace_back(abs(y[i].first - y[i - 1].first), y[i].second,
                   y[i - 1].second);
  }

  sort(ALL(d));

  UnionFind uf = UnionFind(n);

  int t = 0;
  for (const auto e : d) {
    if (uf.isSame(get<1>(e), get<2>(e))) {
      continue;
    }

    t += get<0>(e);
    uf.unite(get<1>(e), get<2>(e));
  }

  cout << t << endl;
}