Farmer John’s hobby of conducting high-energy physics experiments on weekends has backfired, causing N wormholes (2<=N<=12, N even) to materialize on his farm, each located at a distinct point on the 2D map of his farm (the x,y coordinates are both integers).
According to his calculations, Farmer John knows that his wormholes will form N/2 connected pairs. For example, if wormholes A and B are connected as a pair, then any object entering wormhole A will exit wormhole B moving in the same direction, and any object entering wormhole B will similarly exit from wormhole A moving in the same direction. This can have rather unpleasant consequences.
For example, suppose there are two paired wormholes A at (1,1) and B at (3,1), and that Bessie the cow starts from position (2,1) moving in the +x direction. Bessie will enter wormhole B [at (3,1)], exit from A [at (1,1)], then enter B again, and so on, getting trapped in an infinite cycle!

   | . . . .
   | A > B .
   + . . . .

Bessie will travel to B then A then across to B again.
Farmer John knows the exact location of each wormhole on his farm. He knows that Bessie the cow always walks in the +x direction, although he does not remember where Bessie is currently located.
Please help Farmer John count the number of distinct pairings of the wormholes such that Bessie could possibly get trapped in an infinite cycle if she starts from an unlucky position. FJ doesn’t know which wormhole pairs with any other wormhole, so find all the possibilities (i.e., all the different ways that N wormholes could be paired such that Bessie can, in some way, get in a cycle). Note that a loop with a smaller number of wormholes might contribute a number of different sets of pairings to the total count as those wormholes that are not in the loop are paired in many different ways.

PROGRAM NAME
wormhole

INPUT FORMAT
Line 1:The number of wormholes, N.
Lines 2..1+N:Each line contains two space-separated integers describing the (x,y) coordinates of a single wormhole. Each coordinate is in the range 0..1,000,000,000.

SAMPLE INPUT
(file wormhole.in)
4
0 0
1 0
1 1
0 1

INPUT DETAILS
There are 4 wormholes, forming the corners of a square.

OUTPUT FORMAT
Line 1:The number of distinct pairings of wormholes such that Bessie could conceivably get stuck in a cycle walking from some starting point in the +x direction.

SAMPLE OUTPUT
(file wormhole.out)
2

OUTPUT DETAILS
If we number the wormholes 1..4 as we read them from the input, then if wormhole 1 pairs with wormhole 2 and wormhole 3 pairs with wormhole 4, Bessie can get stuck if she starts anywhere between (0,0) and (1,0) or between (0,1) and (1,1).

   | . . . .
   4 3 . . .
   1-2-.-.-.

Bessie will travel to B then A then across to B again
Similarly, with the same starting points, Bessie can get stuck in a cycle if the pairings are 13 and 24 (if Bessie enters WH#3 and comes out at WH#1, she then walks to WH#2 which transports here to WH#4 which directs her towards WH#3 again for a cycle).
Only the pairings 14 and 23 allow Bessie to walk in the +x direction from any point in the 2D plane with no danger of cycling.

题目大意
就是给你一些位置,将这些位置两两配对成虫洞,虫洞是双向的,如果从任意一个位置无限向右(+x)方向走,不能走出去,就说这些虫洞形成了环,询问总共有多少种配对方式能够形成环。

思路
一开始以为这个是图论……其实……这是个枚举……枚举形成的环,然后检测就好了。

代码

/*
PROG:wormhole
LANG:C++
ID:qwerty_3
*/
#include <cstdio>
#include <algorithm>

const int maxn=12;

struct data
{
  int x,y;

  bool operator <(const data &other) const
  {
    if(y!=other.y)
      {
    return y<other.y;
      }
    else
      {
    return x<other.x;
      }
  }
};

data d[maxn+1];
int p[maxn+1],ans,n;

inline int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
    {
      f=-f;
    }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

int ring(int cnt,int now,int begin,int kind)
{
  if((cnt!=1)&&(now==begin)&&(!kind))
    {
      return 1;
    }
  else if(kind)
    {
      return ring(cnt+1,p[now],begin,0);
    }
  else if(d[now+1].y==d[now].y)
    {
      return ring(cnt+1,now+1,begin,1);
    }
  else
    {
      return 0;
    }
};

inline int judge()
{
  for(register int i=1; i<=n; ++i)
    {
      if(ring(1,i,i,0))
    {
      return 1;
    }
    }
  return 0;
}

int pair(int x)
{
  if(x>n)
    {
      ans+=judge();
      return 0;
    }
  if(!p[x])
    {
      for(register int i=x+1; i<=n; ++i)
    {
      if(!p[i])
        {
          p[x]=i;
          p[i]=x;
          pair(x+1);
          p[x]=0;
          p[i]=0;
        }
    }
    }
  else
    {
      pair(x+1);
    }
  return 0;
}

int main()
{
  freopen("wormhole.in","r",stdin);
  freopen("wormhole.out","w",stdout);
  n=read();
  for(register int i=1; i<=n; ++i)
    {
      d[i].x=read();
      d[i].y=read();
    }
  std::sort(d+1,d+n+1);
  pair(1);
  printf("%d\n",ans);
  return 0;
}