Farmer John’s hobby of conducting high-energy physics experiments on weekends has backfired, causing
N
wormholes (
According to his calculations, Farmer John knows that his wormholes will form
N/2
connected pairs. For example, if wormholes
A
and
For example, suppose there are two paired wormholes
A
at
| . . . .
| A > B .
+ . . . .
Bessie will travel to
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
Lines
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
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
| . . . .
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
1−3
and
2−4
(if Bessie enters WH#
3
and comes out at WH#
Only the pairings
题目大意
就是给你一些位置,将这些位置两两配对成虫洞,虫洞是双向的,如果从任意一个位置无限向右(+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;
}