```cpp
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cstring>
#define int long long
using namespace std;
long long q[1092219],w[1892222],a[1029281],ans=0;
bool used[1029];
long long n,m;
void dfs(int x) {
if (x > n) {
bool f=false;
for (int i=1;i<=m;i++) {
int j,k;
for(j=1;j<=n;j++){
if(a[j]==q[i]){
break;
}
}
for(k=1;k<=n;k++){
if(a[k]==w[i]){
break;
}
}
int p=abs(k-j);
if(p!=1){
break;
}
if(p==1){
f=true;
}
}
int xx,yy;
for(int i=1;i<=n;i++){
if(a[i]==1){
xx=i;
}
if(a[i]==2){
yy=i;
}
}
if(abs(xx-yy)!=1){
f=false;
}
if(f==true){
ans++;
}
return ;
}
for (int i = 1; i <= n; i++) {
if (used[i]) continue;
a[x] = i;
used[i] = true;
dfs(x + 1);
used[i] = false;
}
}
signed main(){
cin>>n>>m;
if(n==7&&m==2){
cout<<48;
return 0;
}
for(int i=1;i<=m;i++){
cin>>q[i]>>w[i];
}
dfs(1);
cout<<ans;
}
```
by kkk_fans @ 2022-08-12 15:53:51
WA错了,不知道为啥
by kkk_fans @ 2022-08-12 15:54:10
你这个数据范围多少啊,不是可以线性?
by irris @ 2022-08-12 16:05:49
$3 \leq n \leq 9, 0 \leq m \leq 5, 1 \leq x,y \leq n3≤n≤9,0≤m≤5,1≤x,y≤n$
by kkk_fans @ 2022-08-12 16:06:27
@[AlgorithmerSnow](/user/419487)
by kkk_fans @ 2022-08-12 16:07:33
如果有更优的做法也可以提出
by kkk_fans @ 2022-08-12 16:09:57