Problem p02998 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

const int MOD = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

const int MAX_N = 10010;
vector<int> adj[MAX_N * 2];
bool visited[MAX_N * 2] = {};

VL cnt;

void dfs(int v) {
  if (visited[v] == 1)
    return;
  visited[v] = 1;
  cnt[v / MAX_N]++;

  for (int p : adj[v]) {
    dfs(p);
  }
}

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  REP(i, N) {
    int x, y;
    cin >> x >> y;
    y += MAX_N;
    adj[x].eb(y);
    adj[y].eb(x);
  }
  LL ans = 0;
  for (int i = 0; i < MAX_N * 2; i++) {
    if (visited[i] == 1)
      continue;
    cnt = VL(2, 0);
    dfs(i);
    ans += cnt[0] * cnt[1];
  }
  cout << ans - N << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

const int MOD = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

const int MAX_N = 100010;
vector<int> adj[MAX_N * 2];
bool visited[MAX_N * 2] = {};

VL cnt;

void dfs(int v) {
  if (visited[v] == 1)
    return;
  visited[v] = 1;
  cnt[v / MAX_N]++;

  for (int p : adj[v]) {
    dfs(p);
  }
}

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  REP(i, N) {
    int x, y;
    cin >> x >> y;
    y += MAX_N;
    adj[x].eb(y);
    adj[y].eb(x);
  }
  LL ans = 0;
  for (int i = 0; i < MAX_N * 2; i++) {
    if (visited[i] == 1)
      continue;
    cnt = VL(2, 0);
    dfs(i);
    ans += cnt[0] * cnt[1];
  }
  cout << ans - N << endl;
}