A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement.
How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7.
Input will begin with a line containing N (1 ≤ N ≤ 100000), the number of engineers.
N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2·N. It is guaranteed that no two engineers sit at the same desk.
Print the number of possible assignments, modulo 1000000007 = 109 + 7.
4 1 5 5 2 3 7 7 3
6
5 1 10 2 10 3 10 4 10 5 5
5
These are the possible assignments for the first example:
- 1 5 3 7
- 1 2 3 7
- 5 2 3 7
- 1 5 7 3
- 1 2 7 3
- 5 2 7 3
题意:有2n个位置,每个人有当前的位置和想去的位置,不能冲突。求方案数,每个人可以不换。
题解:裸的并查集,我们对于每个联通块单独计算,如果是一棵树,那么我们有n种方案(每个人都可以只有他换),如果有自环,那么我们不能换,如果是个有环图,那么有两种方案(换或者不换)。
代码:
#include <stdio.h>
int f[200005],sz[200005],n,x,y,u,v,lo[200005],cyc[200005];
int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
int main()
{
scanf("%d",&n);
for(int i=0;i<=2*n;i++) f[i]=i,sz[i]=1;
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
if(x==y) lo[find(x)]=1;
u=find(x),v=find(y);
if(u!=v)
{
f[u]=v;
sz[v]+=sz[u];
lo[v]|=lo[u];
}
else cyc[u]=1;
}
long long ans=1;
for(int i=1;i<=2*n;i++)
if(find(i)==i)
{
if(lo[i]) continue;
if(cyc[i]) ans=ans*2%1000000007;
else ans=ans*sz[i]%1000000007;
}
printf("%I64d\n",ans);
}