AC了,但是觉得不太合理
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
int kkk=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-'){
f=-1;
}
ch=getchar();
}
while(ch>='0'&&ch<='9'){
kkk=kkk*10+ch-'0';
ch=getchar();
}
return f*kkk;
}
int book[55]={},ne[25][3],n,a,ans=0;
void dfs(int now){
if(now==n+1){
ans++;
}
else{
if(book[ne[now][1]]>=2){
book[ne[now][1]]--;dfs(now+1);book[ne[now][1]]++;
}
if(book[ne[now][2]]>=2){
book[ne[now][2]]--;dfs(now+1);book[ne[now][2]]++;
}
}
return ;
}
int main()
{
n=read();if(n==0){printf("0");return 0;}
for(int i=1;i<=n;i++){
a=read();book[a]++;ne[i][1]=a;
a=read();book[a]++;ne[i][2]=a;
}
dfs(1);
printf("%d",ans);
return 0;
}