Problem p02998 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod = 998244353;
const llint big = 2.19e17 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-15;
template <class T, class U> bool mineq(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> bool maxeq(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
llint lcm(llint a, llint b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> void SO(T &ve) { sort(ve.begin(), ve.end()); }
template <class T> void REV(T &ve) { reverse(ve.begin(), ve.end()); }
template <class T> llint LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> llint UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

/// 2009~
int main(void) {
  int n, i, j;
  cin >> n;
  vector<vector<int>> tate(100001);
  vector<vector<int>> yoko(100001);
  vector<int> xz(n);
  vector<int> yz(n);
  for (i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    xz[i] = x;
    yz[i] = y;
    tate[x].pub(i);
    yoko[y].pub(i);
  }
  vector<vector<int>> go(n);
  for (int x = 1; x <= 100000; x++) {
    for (i = 1; i < tate[x].size(); i++) {
      go[tate[x][i]].pub(tate[x][i - 1]);
      go[tate[x][i - 1]].pub(tate[x][i]);
    }
    for (i = 1; i < tate[x].size(); i++) {
      go[yoko[x][i]].pub(yoko[x][i - 1]);
      go[yoko[x][i - 1]].pub(yoko[x][i]);
    }
  }
  vector<bool> mita(n);
  llint ans = -n;
  for (i = 0; i < n; i++) {
    // cerr<<"de"<<endl;
    if (mita[i]) {
      continue;
    }
    set<int> A, B;
    queue<int> que;
    que.push(i);
    while (que.size()) {
      int t = que.front();
      que.pop();
      if (mita[t]) {
        continue;
      }
      mita[t] = 1;

      A.ins(xz[t]);
      B.ins(yz[t]);
      for (auto it : go[t]) {
        que.push(it);
      }
    }
    ans += (llint)A.size() * B.size();
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod = 998244353;
const llint big = 2.19e17 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-15;
template <class T, class U> bool mineq(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> bool maxeq(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
llint lcm(llint a, llint b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> void SO(T &ve) { sort(ve.begin(), ve.end()); }
template <class T> void REV(T &ve) { reverse(ve.begin(), ve.end()); }
template <class T> llint LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> llint UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

/// 2009~
int main(void) {
  int n, i, j;
  cin >> n;
  vector<vector<int>> tate(100001);
  vector<vector<int>> yoko(100001);
  vector<int> xz(n);
  vector<int> yz(n);
  for (i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    xz[i] = x;
    yz[i] = y;
    tate[x].pub(i);
    yoko[y].pub(i);
  }
  vector<vector<int>> go(n);
  for (int x = 1; x <= 100000; x++) {
    for (i = 1; i < tate[x].size(); i++) {
      go[tate[x][i]].pub(tate[x][i - 1]);
      go[tate[x][i - 1]].pub(tate[x][i]);
    }
    for (i = 1; i < yoko[x].size(); i++) {
      go[yoko[x][i]].pub(yoko[x][i - 1]);
      go[yoko[x][i - 1]].pub(yoko[x][i]);
    }
  }
  vector<bool> mita(n);
  llint ans = -n;
  for (i = 0; i < n; i++) {
    // cerr<<"de"<<endl;
    if (mita[i]) {
      continue;
    }
    set<int> A, B;
    queue<int> que;
    que.push(i);
    while (que.size()) {
      int t = que.front();
      que.pop();
      if (mita[t]) {
        continue;
      }
      mita[t] = 1;

      A.ins(xz[t]);
      B.ins(yz[t]);
      for (auto it : go[t]) {
        que.push(it);
      }
    }
    ans += (llint)A.size() * B.size();
  }
  cout << ans << endl;
  return 0;
}