Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#include <iterator>
#include <set>
using namespace std;
// using namespace __gnu_pbds;

#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()

#define ll long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define plii pair<pair<ll, int>, int>
#define piii pair<pii, int>
#define viii vector<pair<pii, int>>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define mp make_pair
#define ms(x, v) memset(x, v, sizeof x)
#define pr_vec(v)                                                              \
  for (int i = 0; i < v.size(); i++)                                           \
    cout << v[i] << " ";

#define f_in(st) freopen(st, "r", stdin)
#define f_out(st) freopen(st, "w", stdout)

#define INF (ll)1e17
#define matrix vector<vector<ll>>

#define fr(i, a, b) for (i = a; i <= b; i++)
#define fb(i, a, b) for (i = a; i >= b; i--)
#define ASST(x, l, r) assert(x <= r && x >= l)
#define maxbits 9
#define maxmask (1ll << maxbits)
// typedef tree<pll, null_type, less<pll>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
const int N = 1e6 + 10, M = (ll)1e9 + 1;
const int mod = (ll)1e9 + 7, sz = 5, MAX = (ll)1e6 + 10;
string fir = "Monocarp", sec = "Bicarp";

vll g[N];

ll dist[N], vis[N];

typedef struct {
  ll x;
  ll y;
  ll id;
} pos_id;

bool cmpx(const pos_id &a, const pos_id &b) { return a.x < b.x; }

bool cmpy(const pos_id &a, const pos_id &b) { return a.y < b.y; }

void mst(ll src) {
  for (ll i = 1; i < N; i++)
    dist[i] = INF;

  set<pair<ll, ll>> s;

  dist[src] = 0;
  s.insert({0, src});
  ll cost = 0;

  while (!s.empty()) {
    auto x = *(s.begin());
    s.erase(x);
    ll u = x.s;
    ll w = x.f;
    vis[u] = true;

    cost += w;

    for (auto &it : g[u]) {
      if (vis[it.f])
        continue;
      if (dist[it.f] > it.s) {
        s.erase({dist[it.f], it.f});
        dist[it.f] = it.s;
        s.insert({it.s, it.f});
      }
    }
  }

  cout << cost;
  return;
}

void work() {
  ll n;
  cin >> n;
  vector<pos_id> point(n);
  for (ll i = 0; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    point[i] = {u, v, i + 1};
  }
  // return;
  sort(point.begin(), point.end(), cmpx);

  ll src;
  for (ll i = 1; i < n; i++) {
    ll u = point[i].id, v = point[i - 1].id, w = point[i].x - point[i - 1].x;
    g[u].pb({v, w});
    g[v].pb({u, w});
    src = u;
  }

  sort(point.begin(), point.end(), cmpy);

  for (ll i = 1; i < n; i++) {
    ll u = point[i].id, v = point[i - 1].id, w = point[i].y - point[i - 1].y;
    g[u].pb({v, w});
    g[v].pb({u, w});
  }

  mst(src);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // precomp();
  ll t;
  // cin >> t;
  t = 1;
  while (t--) {
    work();
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#include <iterator>
#include <set>
using namespace std;
// using namespace __gnu_pbds;

#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()

#define ll long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define plii pair<pair<ll, int>, int>
#define piii pair<pii, int>
#define viii vector<pair<pii, int>>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define mp make_pair
#define ms(x, v) memset(x, v, sizeof x)
#define pr_vec(v)                                                              \
  for (int i = 0; i < v.size(); i++)                                           \
    cout << v[i] << " ";

#define f_in(st) freopen(st, "r", stdin)
#define f_out(st) freopen(st, "w", stdout)

#define INF (ll)1e17
#define matrix vector<vector<ll>>

#define fr(i, a, b) for (i = a; i <= b; i++)
#define fb(i, a, b) for (i = a; i >= b; i--)
#define ASST(x, l, r) assert(x <= r && x >= l)
#define maxbits 9
#define maxmask (1ll << maxbits)
// typedef tree<pll, null_type, less<pll>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
const int N = 1e6 + 10, M = (ll)1e9 + 1;
const int mod = (ll)1e9 + 7, sz = 5, MAX = (ll)1e6 + 10;
string fir = "Monocarp", sec = "Bicarp";

vll g[N];

ll dist[N], vis[N];

typedef struct {
  ll x;
  ll y;
  ll id;
} pos_id;

bool cmpx(const pos_id &a, const pos_id &b) { return a.x < b.x; }

bool cmpy(const pos_id &a, const pos_id &b) { return a.y < b.y; }

void mst(ll src) {
  for (ll i = 1; i < N; i++)
    dist[i] = INF;

  set<pair<ll, ll>> s;

  dist[src] = 0;
  s.insert({0, src});
  ll cost = 0;

  while (!s.empty()) {
    auto x = *(s.begin());
    s.erase(x);
    ll u = x.s;
    ll w = x.f;
    vis[u] = true;

    cost += w;

    for (auto &it : g[u]) {
      if (vis[it.f])
        continue;
      if (dist[it.f] > it.s) {
        s.erase({dist[it.f], it.f});
        dist[it.f] = it.s;
        s.insert({it.s, it.f});
      }
    }
  }

  cout << cost;
  return;
}

void work() {
  ll n;
  cin >> n;
  vector<pos_id> point(n);
  for (ll i = 0; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    point[i] = {u, v, i + 1};
  }
  // return;
  sort(point.begin(), point.end(), cmpx);

  ll src;
  for (ll i = 1; i < n; i++) {
    ll u = point[i].id, v = point[i - 1].id, w = point[i].x - point[i - 1].x;
    g[u].pb({v, w});
    g[v].pb({u, w});
    src = u;
  }

  sort(point.begin(), point.end(), cmpy);

  for (ll i = 1; i < n; i++) {
    ll u = point[i].id, v = point[i - 1].id, w = point[i].y - point[i - 1].y;
    g[u].pb({v, w});
    g[v].pb({u, w});
  }

  mst(src);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll t;
  // cin >> t;
  t = 1;
  while (t--) {
    work();
  }

  return 0;
}