Problem:
Score : 1500 points

Problem Statement

There is an undirected connected graph with N vertices numbered 1 through N.
The lengths of all edges in this graph are 1.
It is known that for each i (1≦i≦N), the distance between vertex 1 and vertex i is A_i, and the distance between vertex 2 and vertex i is B_i.
Determine whether there exists such a graph.
If it exists, find the minimum possible number of edges in it.

Constraints

2≦N≦10^5

0≦A_i,B_i≦N-1

Input

The input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

If there exists a graph satisfying the conditions, print the minimum possible number of edges in such a graph. Otherwise, print -1.

Sample Input 1

4
0 1
1 0
1 1
2 1

Sample Output 1

4

The figure below shows two possible graphs. The graph on the right has fewer edges.

Sample Input 2

3
0 1
1 0
2 2

Sample Output 2

-1

Such a graph does not exist.

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct ll{
  int data;
  int num;
  struct ll *next;
} DL;

DL *createList(int data, int n){
  DL *new;
  new = (DL *)malloc(sizeof(DL));
  int i;
  new->data = data;
  new->num = n;

  return new;
}

void addList(DL **head, DL *add){
  add->next = *head;
  *head = add;
  return;
}

void deleteList(DL **pt){
  DL *del = *pt;
  *pt = del->next;

  free(del);
  return;
}

void printList(DL *head){
  puts("LIST---------------");
  while(head != NULL){
    printf("[%d]%d ", head->num, head->data);
    head = head->next;
  }
  putchar('\n');
  puts("---------------LIST");
  return;
}

void printArray(int *data1, int *data2, int n)
{
  int i;
  printf("d1[ ");
  for(i=0; i<n; i++){
    printf("%d ", data1[i]);
  }
  printf("] d2[ ");
  for(i=0; i<n; i++){
    printf("%d ", data2[i]);
  }
  printf("]\n");
  return;
}

int main()
{
  int N;
  int a, b;
  int find1, find2;
  int i, j, n, index, flg, find;
  int *ss;
  int data1[100000]={0};
  int data2[100000]={0};

  DL *head1=NULL;
  DL *head2=NULL;
  DL **search = NULL;
  scanf("%d", &N);

  // 
  for(i=0; i<N; i++){
    scanf("%d%d", &a, &b);
    find1=find2=0;
    a++;b++;
    if(a==1){
      data1[i]=a;
      find1++;
    }
    if(b==1){
      data2[i]=b;
      find2++;
    }
    for(j=0; j<N; j++){
      if(data1[j] == a-1){
        data1[i]=a;
        find1++;
      }
      if(data2[j] == b-1){
        data2[i]=b;
        find2++;
      }
      if(find1 && find2) break;
    }
    if(j==N){
      if(!find1){
        DL *add = createList(a, i);
        addList(&head1, add);
      }
      if(!find2){
        DL *add = createList(b, i);
        addList(&head2, add);
      }
    }
  }


  // リストの探索で、喋れる言語がなくなるまでループする。
  // 新たに喋れる言語が見つからないとき(flg==0)か、リストのデータがなくなった時(head==NULL)にこのループを終了する。
  find1 = 1;
  while(find1 && head1 != NULL){
    search = &head1;
    find1 = 0;
    while(*search != NULL){
      n = (*search)->num;
      a = (*search)->data;
      for(i=0; i<N; i++){
        if(data1[i] == a-1 && abs(data2[n] - data2[i]) == 1){
          find1++;
          break;
        }
      }
      if(find1){
        data1[n] = a;
        deleteList(search);
      }else{
        search = &(*search)->next;
      }
    }
  }

  if(head1 == NULL){
    find2 = 1;
    while(find2 && head2 != NULL){
      search = &head2;
      find2 = 0;
      while(*search != NULL){
        n = (*search)->num;
        b = (*search)->data;
        for(i=0; i<N; i++){
          if(data2[i] == b-1 && abs(data1[n] - data1[i]) == 1){
            find2++;
            break;
          }
        }
        if(find2){
          data2[n] = b;
          deleteList(search);
        }else{
          search = &(*search)->next;
        }
      }
    }
  }
  printf(head1==NULL && head2==NULL ? "YES" : "NO");
  return(0);
}