Problem:
Score : 600 points

Problem Statement

There are N dots in a two-dimensional plane. The coordinates of the i-th dot are (x_i, y_i).

We will repeat the following operation as long as possible:

Choose four integers a, b, c, d (a \neq c, b \neq d) such that there are dots at exactly three of the positions (a, b), (a, d), (c, b) and (c, d), and add a dot at the remaining position.

We can prove that we can only do this operation a finite number of times. Find the maximum number of times we can do the operation.

Constraints

1 \leq N \leq 10^5

1 \leq x_i, y_i \leq 10^5

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the maximum number of times we can do the operation.

Sample Input 1

3
1 1
5 1
5 5

Sample Output 1

1

By choosing a = 1, b = 1, c = 5, d = 5, we can add a dot at (1, 5). We cannot do the operation any more, so the maximum number of operations is 1.

Sample Input 2

2
10 10
20 20

Sample Output 2

0

There are only two dots, so we cannot do the operation at all.

Sample Input 3

9
1 1
2 1
3 1
4 1
5 1
1 2
1 3
1 4
1 5

Sample Output 3

16

We can do the operation for all choices of the form a = 1, b = 1, c = i, d = j (2 \leq i,j \leq 5), and no more. Thus, the maximum number of operations is 16.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010

class UnionFind {
    public:
        UnionFind(){}
        UnionFind(int size) { Init(size); }
        // size: Union-Find で扱いたい要素の数
        void Init(int size) { m_Data.assign(size, -1); }

        // x と y の属する集合を併合
        // 既に一緒な集合内にいるなら false を
        // 新たに併合されたなら true を返す
        bool Unite(int x, int y) {
            x = Find(x), y = Find(y);
            if(x == y) { return (false); }
            if(m_Data[x] > m_Data[y]) { swap(x, y); }
            m_Data[x] += m_Data[y];
            m_Data[y] = x;
            return (true);
        }

        // 要素 k が属する集合の根を返す
        int Find(int k) {
            if(m_Data[k] < 0) { return (k); }
            return (m_Data[k] = Find(m_Data[k]));
        }

        // 要素 k と同じ集合の要素数を返す
        int Size(int k) { return (-m_Data[Find(k)]); }

        // 要素のうち独立しているグループの数 O(NlogN)
        int GetGroupNum() {
            map<int, bool> groups;
            for(int i = 0; i < m_Data.size(); ++i) { groups[Find(i)] = true; }
            return static_cast<int>(groups.size());
        }

    private:
        vector<int> m_Data;
};

ll N;

ll X[MAX_N];            // id が i のものの x 座標
ll Y[MAX_N];            // id が i のものの y 座標
vector<ll> Xs[MAX_N];   // x 座標が i のものの id 一覧
vector<ll> Ys[MAX_N];   // y 座標が i のものの id 一覧
bool isChecked[MAX_N];  // id が i のものが dfs 済みか
UnionFind uf;
set<ll> group_val_x[MAX_N]; // グループ i で出てくる x 座標のバリエーション
set<ll> group_val_y[MAX_N]; // グループ i で出てくる y 座標のバリエーション


// x 座標もしくは y 座標が同じものを同じグループにする
void dfs(ll id) {
    if(isChecked[id]) { return; }
    isChecked[id] = true;

    ll x = X[id];
    for(auto& next_id : Xs[x]) {
        uf.Unite(id, next_id);
        dfs(next_id);
    }
    ll y = Y[id];
    for(auto& next_id : Ys[y]) {
        uf.Unite(id, next_id);
        dfs(next_id);
    }
}


signed main()
{
    cin >> N;
    REP(i, N) {
        ll x, y;
        cin >> x >> y;
        X[i] = x;
        Y[i] = y;
        Xs[x].push_back(i);
        Ys[y].push_back(i);
    }

    uf.Init(N);
    REP(i, N) { dfs(i); }

    set<ll> valid_group;
    REP(i, N) {
        ll root = uf.Find(i);
        group_val_x[root].insert(X[i]);
        group_val_y[root].insert(Y[i]);
        valid_group.insert(root);
    }

    ll ans = 0;
    for(auto& group_id : valid_group) {
        ans += (group_val_x[group_id].size() * group_val_y[group_id].size()) - uf.Size(group_id);
    }
    printf("%lld\n", ans);

    return 0;
}