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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(non_snake_case)]

fn main() {
    use std::collections::{VecDeque, HashSet};

    let stdin = std::io::stdin();
    let mut stdin = my::io::InputReader::new(stdin.lock());
    
    let N: usize = stdin.read();
    let (x, y): (Vec<i32>, Vec<i32>) = (0..N).map(|_| (stdin.read::<i32>(), stdin.read::<i32>())).unzip();

    // neighbors[i][0]: left (-y) neighbor of i,
    // neighbors[i][1]: right (+y) neighbor of i,
    // neighbors[i][2]: up (-x) neighbor of i,
    // neighbors[i][3]: down (+x) neighbor of i,
    let mut neighbors: Vec<Vec<Option<usize>>> = vec![vec![None; 4]; N];
    let mut xyi: Vec<_> = (0..N).map(|i| (x[i], y[i], i)).collect();
    let mut yxi: Vec<_> = (0..N).map(|i| (y[i], x[i], i)).collect();
    xyi.sort();
    yxi.sort();
    for j in 0..N - 1 {
        let (x1, _y1, i1) = xyi[j];
        let (x2, _y2, i2) = xyi[j + 1];
        if x1 == x2 {
            neighbors[i1][1] = Some(i2);
            neighbors[i2][0] = Some(i1);
        }

        let (y1, _x1, i1) = yxi[j];
        let (y2, _x2, i2) = yxi[j + 1];
        if y1 == y2 {
            neighbors[i1][3] = Some(i2);
            neighbors[i2][2] = Some(i1);
        }
    }

    let mut result: u64 = 0;
    let mut used = vec![false; N];
    for i in 0..N {
        if !used[i] {
            used[i] = true;
            let mut q = VecDeque::<usize>::new();
            let mut xs = HashSet::<i32>::new();
            let mut ys = HashSet::<i32>::new();
            let mut count_points = 1;
            q.push_back(i);
            xs.insert(x[i]);
            ys.insert(y[i]);
            while !q.is_empty() {
                let j: usize = *q.front().unwrap();
                q.pop_front();
                for dir in 0..4 {
                    if let Some(k) = neighbors[j][dir] {
                        if !used[k] {
                            used[k] = true;
                            q.push_back(k);
                            xs.insert(x[k]);
                            ys.insert(y[k]);
                            count_points += 1;
                        }
                    }
                }
            }
            result += xs.len() as u64 * ys.len() as u64 - count_points;
        }
    }
    println!("{}", result);
}

#[allow(dead_code, unused_must_use)]
mod my {
    pub mod io {
        use std::io::{BufRead, ErrorKind, Result};
        use std::str::{FromStr, from_utf8};
        use std::fmt::Debug;

        pub fn read_word<R: BufRead>(r: &mut R, buf: &mut Vec<u8>)
                                              -> Result<usize> {
            let mut read = 0;
            loop {
                let (done, used) = {
                    let available = match r.fill_buf() {
                        Ok(n) => n,
                        Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
                        Err(e) => return Err(e)
                    };
                    match available.iter().position(|&x| x == b'\n' || x == b' ') {
                        Some(i) => {
                            buf.extend_from_slice(&available[..i]);
                            (true, i + 1)
                        }
                        None => {
                            buf.extend_from_slice(available);
                            (false, available.len())
                        }
                    }
                };
                r.consume(used);
                read += used;
                if done || used == 0 {
                    return Ok(read);
                }
            }
        }

        pub struct InputReader<R: BufRead> {
            reader: R,
            buf: Vec<u8>,
        }

        impl<R: BufRead> InputReader<R> {
            pub fn new(reader: R) -> InputReader<R> {
                InputReader {
                    reader: reader,
                    buf: Vec::<u8>::new(),
                }
            }

            pub fn read_word(&mut self) {
                read_word(&mut self.reader, &mut self.buf);
            }
            
            pub fn read<T>(&mut self) -> T
                where T: FromStr,
                      T::Err: Debug
            {
                self.buf.clear();
                self.read_word();
                from_utf8(&self.buf).unwrap().trim().parse().unwrap()
            }
        }
    }
}