思路:总的来说,就是按边dfs+剪枝,具体见代码注释。
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#define N 10
#define M 30
#define LL long long
#define mod 1000000007
using namespace std;
int n, m;
int ans;
int num[N];
int on[N], off[N]; //第i个人online朋友的个数和offline朋友的个数
struct Edge{
int a, b;
}edge[M];
void init(){
memset(num, 0, sizeof num);
memset(on, 0, sizeof on);
memset(off, 0, sizeof off);
ans = 0;
cin >> n >> m;
for (int i = 0; i < m; i++){
int a, b;
scanf("%d %d", &a, &b);
edge[i].a = a, edge[i].b = b;
num[a]++, num[b]++;
}
return;
}
void dfs(int e){
if (e == m){
ans++;
return;
}
int a = edge[e].a;
int b = edge[e].b;
num[a]--, num[b]--;
on[a]++; on[b]++;
int flag = false;
if (num[a] && num[b])flag = true;
if (!num[a] && num[b] && on[a] == off[a])flag = true;
if (num[a] && !num[b] && on[b] == off[b])flag = true;
if (!num[a] && !num[b] && on[a] == off[a] && on[b] == off[b])flag = true;
if (flag)dfs(e + 1);
on[a]--, on[b]--, flag = false;
off[a]++; off[b]++;
if (num[a] && num[b])flag = true;
if (!num[a] && num[b] && on[a] == off[a])flag = true;
if (num[a] && !num[b] && on[b] == off[b])flag = true;
if (!num[a] && !num[b] && on[a] == off[a] && on[b] == off[b])flag = true;
if (flag)dfs(e + 1);
off[a]--; off[b]--;
num[a] ++; num[b]++;
return;
}
int main(){
int t;
cin >> t;
while (t--){
init();
dfs(0);
cout << ans << endl;
}
return 0;
}