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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;
//import java.math.*;
//import java.awt.Point;
 
public class Main {
	//static final long MOD = 998244353L;
	//static final long INF = -1000000000000000007L;
	static final long MOD = 1000000007L;
	//static final int INF = 1000000007;
	
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		int N = sc.ni();
		int[][] points = new int[N][2];
		ArrayList<Integer> Xs = new ArrayList<Integer>();
		ArrayList<Integer> Ys = new ArrayList<Integer>();
		for (int i = 0; i < N; i++) {
			points[i][0] = sc.ni();
			Xs.add(points[i][0]);
			points[i][1] = sc.ni();
			Ys.add(points[i][1]);
		}
		HashMap<Integer,Integer> xMap = new HashMap<Integer,Integer>();
		HashMap<Integer,Integer> yMap = new HashMap<Integer,Integer>();
		Collections.sort(Xs);
		int xI = 0;
		for (int i = 0; i < N; i++) {
			if (i == 0 || Xs.get(i) > Xs.get(i-1)) {
				xMap.put(Xs.get(i),xI);
				xI++;
			}
		}
		Collections.sort(Ys);
		int yI = 0;
		for (int i = 0; i < N; i++) {
			if (i == 0 || Ys.get(i) > Ys.get(i-1)) {
				yMap.put(Ys.get(i),yI);
				yI++;
			}
		}
		ArrayList<Integer>[] graph = new ArrayList[xI+yI];
		for (int i = 0; i < xI+yI; i++)
			graph[i] = new ArrayList<Integer>();
		for (int i = 0; i < N; i++) {
			points[i][0] = xMap.get(points[i][0]);
			points[i][1] = yMap.get(points[i][1])+xI;
			
			graph[points[i][0]].add(points[i][1]);
			graph[points[i][1]].add(points[i][0]);
		}
		boolean[] visited = new boolean[xI+yI];
		long ans = 0;
		ArrayDeque<Integer> bfs = new ArrayDeque<Integer>();
		for (int root = 0; root < xI+yI; root++) {
			if (visited[root])
				continue;
			//Find the number of xNodes, yNodes, and edges in this component
			int xCnt = 0;
			int yCnt = 0;
			int e = 0;
			visited[root] = true;
			bfs.add(root);
			while (! bfs.isEmpty()) {
				int node = bfs.poll();
				if (node < xI)
					xCnt++;
				else
					yCnt++;
				for (int neighbor: graph[node]) {
					if (! visited[neighbor]) {
						e++;
						visited[neighbor] = true;
						bfs.add(neighbor);
					}
				}
			}
			
			//According to a "well known" statement, the number of added points is (xCnt*yCnt)-e.
			long add = (xCnt+0L)*yCnt-e;
			ans += add;
		}
		
		pw.println(ans);
		pw.close();
	}
	
	
	public static long dist(long[] p1, long[] p2) {
		return (Math.abs(p2[0]-p1[0])+Math.abs(p2[1]-p1[1]));
	}
	
	//Find the GCD of two numbers
	public static long gcd(long a, long b) {
		if (a < b) return gcd(b,a);
		if (b == 0)
			return a;
		else
			return gcd(b,a%b);
	}
	
	//Fast exponentiation (x^y mod m)
	public static long power(long x, long y, long m) { 
		long ans = 1;
		x %= m;
		while (y > 0) { 
			if(y % 2 == 1) 
				ans = (ans * x) % m; 
			y /= 2;  
			x = (x * x) % m;
		} 
		return ans; 
	}
	
	public static int[][] shuffle(int[][] array) {
		Random rgen = new Random();
		for (int i = 0; i < array.length; i++) {
		    int randomPosition = rgen.nextInt(array.length);
		    int[] temp = array[i];
		    array[i] = array[randomPosition];
		    array[randomPosition] = temp;
		}
		return array;
	}
	
    public static int[][] sort(int[][] array) {
    	//Sort an array (immune to quicksort TLE)
 
		Arrays.sort(array, new Comparator<int[]>() {
			  @Override
        	  public int compare(int[] arr1, int[] arr2) {
				  return arr1[1]-arr2[1]; //ascending order
	          }
		});
		return array;
	}
    
    public static long[][] sort(long[][] array) {
    	//Sort an array (immune to quicksort TLE)
		Random rgen = new Random();
		for (int i = 0; i < array.length; i++) {
		    int randomPosition = rgen.nextInt(array.length);
		    long[] temp = array[i];
		    array[i] = array[randomPosition];
		    array[randomPosition] = temp;
		}
		Arrays.sort(array, new Comparator<long[]>() {
			  @Override
        	  public int compare(long[] arr1, long[] arr2) {
				  //return 0;
				  if (arr1[0] < arr2[0]) {
					  return -1;
				  } else if (arr1[0] > arr2[0]) {
					  return 1;
				  } else {
					  return 0;
				  }
	          }
		});
		return array;
	}
    
    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int ni() { 
            return Integer.parseInt(next()); 
        } 
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() { 
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    }
}