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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC131.F();
        }
        class ABC131
        {
            public static void A()
            {
                var S = Read.Str();
                Console.WriteLine(S[0] == S[1] || S[1] == S[2] || S[2] == S[3] ? "Bad" : "Good");
            }
            public static void B()
            {
                var N = Read.Int();
                var L = Read.Int();
                var min = L;
                var max = L + N - 1;
                var total = (min + max) * N / 2;
                if (min > 0)
                {
                    total -= min;
                }
                if (max < 0)
                {
                    total -= max;
                }
                Console.WriteLine(total);
            }
            public static void C()
            {
                var A = Read.Long();
                var B = Read.Long();
                var C = Read.Long();
                var D = Read.Long();
                var lcm = C * D / Util.GCD(C, D);
                var total = B - A + 1;
                var divC = B / C - (A - 1) / C;
                var divD = B / D - (A - 1) / D;
                var divCD = B / lcm - (A - 1) / lcm;
                Console.WriteLine(total - divC - divD + divCD);
            }
            public static void D()
            {
                var N = Read.Int();
                var AB = Read.TupleInt(N);
                AB = AB.OrderBy(x => x.Item2).ToArray();
                int current = 0;
                bool ok = true;
                for(int i = 0; i < N; ++i)
                {
                    current += AB[i].Item1;
                    if (current > AB[i].Item2)
                    {
                        ok = false;
                        break;
                    }
                }
                Console.WriteLine(ok ? "Yes" : "No");
            }
            public static void E()
            {
                var N = Read.Int();
                var K = Read.Int();
                if (K > (N - 1) * (N - 2) / 2) { Console.WriteLine(-1); return; }
                var list = new List<Tuple<int, int>>();
                for (int i = 2; i <= N; ++i)
                {
                    list.Add(new Tuple<int, int>(1, i));
                }
                int a = 2;
                int b = 3;
                int kill = (N - 1) * (N - 2) / 2 - K;
                for (int i = 0; i < kill; ++i)
                {
                    list.Add(new Tuple<int, int>(a, b));
                    b++;
                    if (b > N) { a++; b = a + 1; }
                }
                Console.WriteLine(list.Count);
                foreach(var t in list)
                {
                    Console.WriteLine("{0} {1}", t.Item1, t.Item2);
                }
            }
            public static void F()
            {
                var N = Read.Int();
                var XY = Read.TupleInt(N);
                Dictionary<int, HashSet<int>> dictX = new Dictionary<int, HashSet<int>>();
                Dictionary<int, HashSet<int>> dictY = new Dictionary<int, HashSet<int>>();
                for(int i = 0; i < N; ++i)
                {
                    var x = XY[i].Item1;
                    var y = XY[i].Item2;
                    if (!dictX.ContainsKey(x)) { dictX.Add(x, new HashSet<int>()); }
                    dictX[x].Add(i);
                    if (!dictY.ContainsKey(y)) { dictY.Add(y, new HashSet<int>()); }
                    dictY[y].Add(i);
                }

                HashSet<int> checkI = new HashSet<int>();
                HashSet<int> checkX = new HashSet<int>();
                HashSet<int> checkY = new HashSet<int>();
                long sum = 0;
                for(int i = 0; i < N; ++i)
                {
                    if (checkI.Contains(i)) { continue; }
                    HashSet<int> setI = new HashSet<int>();
                    HashSet<int> setX = new HashSet<int>();
                    HashSet<int> setY = new HashSet<int>();
                    Queue<int> qX = new Queue<int>();
                    Queue<int> qY = new Queue<int>();
                    setI.Add(i);
                    qX.Enqueue(XY[i].Item1);
                    qY.Enqueue(XY[i].Item2);
                    while (qX.Count + qY.Count > 0)
                    {
                        if (qX.Count > 0)
                        {
                            var x = qX.Dequeue();
                            foreach(var j in dictX[x])
                            {
                                if (!checkI.Contains(j))
                                {
                                    checkI.Add(j);
                                    setI.Add(j);
                                    var jX = XY[j].Item1;
                                    var jY = XY[j].Item2;
                                    if (!setX.Contains(jX)) { setX.Add(jX); qX.Enqueue(jX); }
                                    if (!setY.Contains(jY)) { setY.Add(jY); qY.Enqueue(jY); }
                                }
                            }
                        }
                        if (qY.Count > 0)
                        {
                            var y = qY.Dequeue();
                            foreach (var j in dictY[y])
                            {
                                if (!checkI.Contains(j))
                                {
                                    checkI.Add(j);
                                    setI.Add(j);
                                    var jX = XY[j].Item1;
                                    var jY = XY[j].Item2;
                                    if (!setX.Contains(jX)) { setX.Add(jX); qX.Enqueue(jX); }
                                    if (!setY.Contains(jY)) { setY.Add(jY); qY.Enqueue(jY); }
                                }
                            }
                        }
                    }
                    sum += (long)setX.Count * setY.Count - setI.Count;
                }
                Console.WriteLine(sum);
            }
        }

        class Util
        {
            public static long GCD(long a, long b)
            {
                while (b > 0)
                {
                    var c = a % b;
                    a = b;
                    b = c;
                }
                return a;
            }
        }

        static class Read
        {
            static Queue<string> queue = new Queue<string>();
            static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
            public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
            public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
            public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
            public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
            public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
            public static string[] StrN() { return Console.ReadLine().Split(' '); }
            public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
            public static long[,] LongMatrix(int r, int c)
            {
                var mat = new long[r, c];
                for (int i = 0; i < r; ++i)
                {
                    var x = Read.LongN();
                    for (int j = 0; j < c; ++j)
                    {
                        mat[i, j] = x[j];
                    }
                }
                return mat;
            }
            public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M)
            {
                Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new HashSet<int>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1]);
                    dict[ab[1]].Add(ab[0]);
                }
                return dict;
            }
            public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
            {
                Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new Dictionary<int, long>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1], ab[2]);
                    dict[ab[1]].Add(ab[0], ab[2]);
                }
                return dict;
            }
        }
    }
}