Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define case(i) printf("Case #%d: ", i);
using namespace std;
typedef long long int ll;
typedef complex<double> com;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int MOD = 998244353;
const ll INF = 4e18;

class UnionFind {
public:
  vector<int> par;
  UnionFind(int n) { par = vector<int>(n, -1); }
  int root(int a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = root(par[a]);
    }
  }
  int size(int a) { return -par[root(a)]; }
  bool connect(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b) {
      return false;
    }
    if (size(a) < size(b)) {
      swap(a, b);
    }
    par[a] += par[b];
    par[b] = a;
    return true;
  }
};

struct edge {
  int u, v, cost;
};
bool comp(edge &e1, edge &e2) { return e1.cost < e2.cost; }
#define MAX_E 100010
edge es[MAX_E];
int V, E;
int kruskal() {
  sort(es, es + E, comp);
  UnionFind uni(V);
  int res = 0;
  rep(i, 0, E) {
    edge e = es[i];
    if (uni.root(e.u) != uni.root(e.v)) {
      uni.connect(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

int main() {
  cin >> V;
  E = 0;
  vector<P> x, y;
  rep(i, 0, V) {
    int p1, p2;
    cin >> p1 >> p2;
    x.push_back({p1, i});
    y.push_back({p2, i});
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  rep(i, 0, V - 1) {
    E++;
    es[i] = {x[i].second, x[i + 1].second, x[i + 1].first - x[i].first};
  }
  rep(i, 0, V - 1) {
    E++;
    es[i + V - 1] = {y[i].second, y[i + 1].second, y[i + 1].first - y[i].first};
  }
  printf("%d", kruskal());
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define case(i) printf("Case #%d: ", i);
using namespace std;
typedef long long int ll;
typedef complex<double> com;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int MOD = 998244353;
const ll INF = 4e18;

class UnionFind {
public:
  vector<int> par;
  UnionFind(int n) { par = vector<int>(n, -1); }
  int root(int a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = root(par[a]);
    }
  }
  int size(int a) { return -par[root(a)]; }
  bool connect(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b) {
      return false;
    }
    if (size(a) < size(b)) {
      swap(a, b);
    }
    par[a] += par[b];
    par[b] = a;
    return true;
  }
};

struct edge {
  int u, v, cost;
};
bool comp(edge &e1, edge &e2) { return e1.cost < e2.cost; }
#define MAX_E 200010
edge es[MAX_E];
int V, E;
int kruskal() {
  sort(es, es + E, comp);
  UnionFind uni(V);
  int res = 0;
  rep(i, 0, E) {
    edge e = es[i];
    if (uni.root(e.u) != uni.root(e.v)) {
      uni.connect(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

int main() {
  cin >> V;
  E = 0;
  vector<P> x, y;
  rep(i, 0, V) {
    int p1, p2;
    cin >> p1 >> p2;
    x.push_back({p1, i});
    y.push_back({p2, i});
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  rep(i, 0, V - 1) {
    E++;
    es[i] = {x[i].second, x[i + 1].second, x[i + 1].first - x[i].first};
  }
  rep(i, 0, V - 1) {
    E++;
    es[i + V - 1] = {y[i].second, y[i + 1].second, y[i + 1].first - y[i].first};
  }
  printf("%d", kruskal());
  return 0;
}