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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            var N = consoleReader.Int;
            var points = consoleReader.Repeat(N).SplitInt.Select(Point.Create).ToArray();
            var xPoints = new SortedList<int, HashSet<Point>>();
            var yPoints = new SortedList<int, HashSet<Point>>();
            foreach (var p in points)
            {
                HashSet<Point> xp, yp;
                xPoints.TryGetValue(p.x, out xp);
                if (xp == null)
                    xPoints.Add(p.x, new HashSet<Point> { p });
                else
                    xp.Add(p);
                yPoints.TryGetValue(p.y, out yp);
                if (yp == null)
                    yPoints.Add(p.y, new HashSet<Point> { p });
                else
                    yp.Add(p);
            }

            var pointSet = new HashSet<Point>(points);

            long count = 0;
            for (int i = 0; i < xPoints.Count; i++)
            {
                foreach (var p1 in xPoints.Values[i])
                {
                    for (int j = i + 1; j < xPoints.Count; j++)
                    {
                        foreach (var p2 in xPoints.Values[j])
                        {
                            var b1 = pointSet.Contains(new Point { x = p1.x, y = p2.y });
                            var b2 = pointSet.Contains(new Point { x = p2.x, y = p1.y });
                            if (b1 && !b2 || !b1 && b2)
                            {
                                count++;
                            }
                        }
                    }
                }
            }

            return count;
        }
        struct Point : IEquatable<Point>
        {
            public static Point Create(int[] l)
            {
                return new Point { x = l[0], y = l[1] };
            }
            public int x;
            public int y;
            public override string ToString() => $"{x} {y}";
            public bool Equals(Point other) => this.x == other.x && this.y == other.y;
            public override bool Equals(object obj)
            {
                if (obj is Point)
                    return this.Equals((Point)obj);
                else return false;
            }
            public override int GetHashCode()
            {
                var hashCode = 1502939027;
                hashCode = hashCode * -1521134295 + this.x.GetHashCode();
                hashCode = hashCode * -1521134295 + this.y.GetHashCode();
                return hashCode;
            }
        }


        #region いつもの
        private readonly ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader()).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
        #endregion
    }

    public class ConsoleReader { public class SplitLine { string[] splited; public SplitLine(string line) { splited = line.Split(); } public string[] String => splited.ToArray(); public int[] Int => splited.Select(int.Parse).ToArray(); public long[] Long => splited.Select(long.Parse).ToArray(); public ulong[] ULong => splited.Select(ulong.Parse).ToArray(); public double[] Double => splited.Select(double.Parse).ToArray(); } public class RepeatLine { ConsoleReader reader; int num; public RepeatLine(ConsoleReader reader, int num) { this.reader = reader; this.num = num; } public IEnumerable<string> String { get { for (int i = 0; i < num; i++) yield return reader.String; } } public IEnumerable<int> Int { get { for (int i = 0; i < num; i++) yield return reader.Int; } } public IEnumerable<long> Long { get { for (int i = 0; i < num; i++) yield return reader.Long; } } public IEnumerable<string[]> SplitString { get { for (int i = 0; i < num; i++) yield return reader.Split.String; } } public IEnumerable<int[]> SplitInt { get { for (int i = 0; i < num; i++) yield return reader.Split.Int; } } } public virtual string String => Console.ReadLine(); public int Int => int.Parse(String); public long Long => long.Parse(String); public ulong ULong => ulong.Parse(String); public double Double => double.Parse(String); public SplitLine Split => new SplitLine(String); public RepeatLine Repeat(int count) => new RepeatLine(this, count); }
}