Problem:
Problem D: King Slime

There is a grid of size W × H surrounded by walls. Some cells in the grid are occupied by slimes. The slimes want to unite with each other and become a "King Slime".

In each move, a slime can move to east, west, south and north direction until it enters a cell occupied by another slime or hit the surrounding wall. If two slimes come together, they unite and become a new slime.

Your task is write a program which calculates the minimum number of moves that all the slimes unite and become a King Slime. Suppose slimes move one by one and they never move simultaneously.

Input

The first line contains three integers N (2 ≤ N ≤ 40,000), W and H (1 ≤ W, H ≤ 100,000), which denote the
number of slimes, the width and the height of the grid respectively.

The following N lines describe the initial coordinates of the slimes. The i-th line contains two integers xi (1 ≤ xi ≤ W) and yi (1 ≤ yi ≤ H), which indicate the coordinates of the i-th slime . All the coordinates are 1-based.

You may assume that each cell is occupied by at most one slime initially.

Output

Output the minimum number of moves that all the slimes unite and become a King Slime.

Sample Input 1

4 3 3
1 1
1 3
3 1
3 3

Output for the Sample Input 1

3

Sample Input 2

2 3 3
2 2
3 3

Output for the Sample Input 2

2

Sample Input 3

2 4 4
2 2
3 3

Output for the Sample Input 3

3

Sample Input 4

2 4 4
2 2
2 3

Output for the Sample Input 4

1

Solution in C++11 (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

class UFT {
public:
  vector<int> parent;
  vector<int> rank;
  vector<int> size;
  UFT(int n){
    parent = vector<int>(n);
    rank = vector<int>(n,0);
    size = vector<int>(n,1);
    for(int i = 0; i < n; i++)
      parent[i] = i;
  }
  int find_root(int x){
    if ( parent[x] == x ) return x;
    return parent[x] = find_root( parent[x] );
  }
  void unite(int x, int y){
    int u = find_root(x);
    int v = find_root(y);
    if ( u == v ) return;
    if ( rank[u] < rank[v] ){
      parent[u] = v;
      size[v] += size[u];
    }else{
      parent[v] = u;
      size[u] += size[v];
      if ( rank[u] == rank[v] ) rank[x]++;
    }
  }
  bool is_same ( int x, int y ){
    return find_root(x) == find_root(y);
  }
  int size_of ( int x ){
    return size[ find_root(x) ];
  }
};

typedef long long Cost;
class edge{
public:
  int u,v;;
  Cost cost;
  edge(int u, int v, Cost c) : u(u), v(v), cost(c) { ; }
  bool operator< (const edge &e) const {
    return cost < e.cost;
  }
};
class SpanningTree{
public:
  int V;
  vector< edge > edges;
  SpanningTree(int n) : V(n) {; }
  void add_edge(int v, int u, Cost c){
    edges.push_back( edge(v,u,c) );
  }
  Cost kruskal(){
    sort( edges.begin(), edges.end() );
    UFT uft(V);
    Cost ret = 0;
    for(int i = 0; i < edges.size(); i++){
      edge &e = edges[i];
      if( !uft.is_same(e.u, e.v) ){
	uft.unite(e.u, e.v);
	ret += e.cost;
      }
    }
    return ret;
  }
};

int N, W, H;
vector<tuple<long long, long long, int>> p;
vector<tuple<long long, long long, int>> q;
vector<tuple<long long, long long>> s;
int M[40000];
int main(){
  cin >> N >> W >> H;
  for(int i = 0; i < N; i++){
    long long x, y;
    cin >> x >> y;
    p.emplace_back(x,y,i);
    q.emplace_back(y,x,i);
    s.emplace_back(y,x);
  }
  UFT uft(N);
  SpanningTree st(N);
  sort(p.begin(), p.end());
  sort(q.begin(), q.end());

  for(int i = 1; i < N; i++){
    if( get<0>(p[i-1]) == get<0>(p[i]) ){
      uft.unite( get<2>(p[i-1]), get<2>(p[i]) );
      st.add_edge( get<2>(p[i-1]), get<2>(p[i]), 1 );
    }
    if( get<0>(q[i-1]) == get<0>(q[i]) ){
      uft.unite( get<2>(q[i-1]), get<2>(q[i]) );
      st.add_edge( get<2>(q[i-1]), get<2>(q[i]), 1 );
    }
  }
  long long sum_of_componentwise = st.kruskal();
  //cout << sum_of_componentwise << endl;
  int C = 0;
  for(int i = 0; i < N; i++){
    if( uft.parent[i] == i ){
      M[i] = C;
      C++;
    }
  }
  for(int i = 0; i < N; i++){
    if( uft.parent[i] != i ){
      M[i] = M[uft.find_root(i)];
    }
  }
  long long w0 = 0, wW = 0, h0 = 0, hH = 0;
  for(int i = 0; i < N; i++){
    long long x, y;
    tie(x,y) = s[i];
    if( x == 1 ){
      w0++;
    }else if( x == W ){
      wW++;
    }
    if( y == 1 ){
      h0++;
    }else if( y == H ){
      hH++;
    }
  }

  long long ans = 1e18;
  if( C == 1 ){
    ans = 0;
  }else{
    ans = min( ans, (C-w0) + C-1);
    ans = min( ans, (C-wW) + C-1);
    ans = min( ans, (C-h0) + C-1);
    ans = min( ans, (C-hH) + C-1);
  }
  cout << ans + sum_of_componentwise << endl;
}