Problem p02998 (C++)

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define debug(x) cerr << "\n" << #x << ": " << x << "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e9
#define pii pair<int, int>
#define pt(xx) cout << "\n" << xx << "\n"
#define debugv(x, n)                                                           \
  cout << "\n" << #x << ": ";                                                  \
  rep(i, n) cout << x[i] << " ";                                               \
  cout << endl
#define debug2d(x, m, n)                                                       \
  cout << "\n" << #x << " is: \n";                                             \
  for (int i = 0; i < m; ++i) {                                                \
    for (int j = 0; j < n; ++j) {                                              \
      cout << x[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }
#define nl cout << endl

#define MOD 1000000007

int N, K, L;
int ans;

const int V = 10005;
vector<vector<int>> v(2 * V);
vector<int> visited(2 * V, 0);
int cnt[2];

void dfs(int n) {
  if (visited[n])
    return;
  visited[n] = 1;

  if (n < V)
    cnt[0]++;
  else
    cnt[1]++;

  for (auto x : v[n]) {
    dfs(x);
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  //==============================================

  ans = 0;
  cin >> N;
  int x, y;
  rep(i, N) {
    cin >> x >> y;
    y = y + V;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  rep(i, 2 * V) {
    // if(v[i].size() < 1)continue;
    if (visited[i])
      continue;
    cnt[0] = 0;
    cnt[1] = 0;

    dfs(i);
    ans += cnt[0] * cnt[1];
  }

  ans -= N;
  cout << ans << endl;

  return 0;
}

Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define debug(x) cerr << "\n" << #x << ": " << x << "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define int long long
#define INF 1e9
#define pii pair<int, int>
#define pt(xx) cout << "\n" << xx << "\n"
#define debugv(x, n)                                                           \
  cout << "\n" << #x << ": ";                                                  \
  rep(i, n) cout << x[i] << " ";                                               \
  cout << endl
#define debug2d(x, m, n)                                                       \
  cout << "\n" << #x << " is: \n";                                             \
  for (int i = 0; i < m; ++i) {                                                \
    for (int j = 0; j < n; ++j) {                                              \
      cout << x[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }
#define nl cout << endl

#define MOD 1000000007

int N, K, L;
int ans;

const int V = 100005;
vector<vector<int>> v(2 * V);
vector<int> visited(2 * V, 0);
int cnt[2];

void dfs(int n) {
  if (visited[n])
    return;
  visited[n] = 1;

  if (n < V)
    cnt[0]++;
  else
    cnt[1]++;

  for (auto x : v[n]) {
    dfs(x);
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  //==============================================

  ans = 0;
  cin >> N;
  int x, y;
  rep(i, N) {
    cin >> x >> y;
    y = y + V;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  rep(i, 2 * V) {
    // if(v[i].size() < 1)continue;
    if (visited[i])
      continue;
    cnt[0] = 0;
    cnt[1] = 0;

    dfs(i);
    ans += cnt[0] * cnt[1];
  }

  ans -= N;
  cout << ans << endl;

  return 0;
}