Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
static const double EPS = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof(a))
#define minus(a) memset(a, 0xff, sizeof(a))
#define chmax(a, b) a = min((a), (b))
#define chmin(a, b) a = max((a), (b))

#define MOD 1000000007

typedef pair<int, int> P;
#define INF (1 << 29)

struct edge {
  int to, cost;
  edge(int to, int cost) {
    this->to = to;
    this->cost = cost;
  }
};

int V, E;
typedef vector<P> edges;
vector<edges> G;

int n;
int visit[105];

void go(int v) {
  priority_queue<P> q; //
  q.push(P(0, 0));
  ll ans = 0;
  while (q.size()) {
    P u = q.top();
    q.pop();
    if (visit[u.second] == 1)
      continue;
    visit[u.second] = 1;
    ans += -u.first;
    for (auto w : G[u.second]) {
      if (visit[w.second] == 1)
        continue;
      q.push(w);
    }
  }
  cout << ans << endl;
}

int main() {
  scanf("%d ", &n);
  G.resize(n);
  vector<P> p1, p2;
  int a, b;
  rep(i, n) {
    scanf("%d %d ", &a, &b);
    a--;
    b--;
    p1.push_back(P(a, i));
    p2.push_back(P(b, i));
  }
  sort(p1.begin(), p1.end());
  sort(p2.begin(), p2.end());
  rep(i, n - 1) {
    G[p1[i].second].push_back(
        P(p1[i].first - p1[i + 1].first, p1[i + 1].second));
    G[p1[i + 1].second].push_back(
        P(p1[i].first - p1[i + 1].first, p1[i].second));
    G[p2[i].second].push_back(
        P(p2[i].first - p2[i + 1].first, p2[i + 1].second));
    G[p2[i + 1].second].push_back(
        P(p2[i].first - p2[i + 1].first, p2[i].second));
  }
  go(0);

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
static const double EPS = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof(a))
#define minus(a) memset(a, 0xff, sizeof(a))
#define chmax(a, b) a = min((a), (b))
#define chmin(a, b) a = max((a), (b))

#define MOD 1000000007

typedef pair<int, int> P;
#define INF (1 << 29)

struct edge {
  int to, cost;
  edge(int to, int cost) {
    this->to = to;
    this->cost = cost;
  }
};

int V, E;
typedef vector<P> edges;
vector<edges> G;

int n;
int visit[100005]; //

void go(int v) {
  priority_queue<P> q; //
  q.push(P(0, 0));
  ll ans = 0;
  while (q.size()) {
    P u = q.top();
    q.pop();
    if (visit[u.second] == 1)
      continue;
    visit[u.second] = 1;
    ans += -u.first;
    for (auto w : G[u.second]) {
      if (visit[w.second] == 1)
        continue;
      q.push(w);
    }
  }
  cout << ans << endl;
}

int main() {
  scanf("%d ", &n);
  G.resize(n);
  vector<P> p1, p2;
  int a, b;
  rep(i, n) {
    scanf("%d %d ", &a, &b);
    a--;
    b--;
    p1.push_back(P(a, i));
    p2.push_back(P(b, i));
  }
  sort(p1.begin(), p1.end());
  sort(p2.begin(), p2.end());
  rep(i, n - 1) {
    G[p1[i].second].push_back(
        P(p1[i].first - p1[i + 1].first, p1[i + 1].second));
    G[p1[i + 1].second].push_back(
        P(p1[i].first - p1[i + 1].first, p1[i].second));
    G[p2[i].second].push_back(
        P(p2[i].first - p2[i + 1].first, p2[i + 1].second));
    G[p2[i + 1].second].push_back(
        P(p2[i].first - p2[i + 1].first, p2[i].second));
  }
  go(0);

  return 0;
}