Farmer John’s hobby of conducting high-energy physics experiments on weekends has backfired, causing
According to his calculations, Farmer John knows that his wormholes will form
For example, suppose there are two paired wormholes
| . . . .
| 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
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
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
OUTPUT FORMAT
Line
SAMPLE OUTPUT
(file wormhole.out)
2
OUTPUT DETAILS
If we number the wormholes
| . . . .
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
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;
}