Problem Description
There are n people and m pairs of friends. For every pair of friends, they can choose to become online friends (communicating using online applications) or offline friends (mostly using face-to-face communication). However, everyone in these n people wants to have the same number of online and offline friends (i.e. If one person has x onine friends, he or she must have x offline friends too, but different people can have different number of online or offline friends). Please determine how many ways there are to satisfy their requirements.
Input
The first line of the input is a single integer T (T=100), indicating the number of testcases.
For each testcase, the first line contains two integers n (1≤n≤8) and m (0≤m≤n(n−1)2), indicating the number of people and the number of pairs of friends, respectively. Each of the next m lines contains two numbers x and y, which mean x and y are friends. It is guaranteed that x≠y and every friend relationship will appear at most once.
Output
For each testcase, print one number indicating the answer.
Sample Input
2
3 3
1 2
2 3
3 1
4 4
1 2
2 3
3 4
4 1
Sample Output
2
题意:给出一幅无向图。依次对边染白色或黑色,使得每个点所关联的白边和黑边数目相同。问有多少种染色方法。
思路:
搜索题。强行暴力搜必定超时。枚举点来搜索也不好写。因此枚举边。
记录每个点的度数,若有点的度数为奇数,直接输出0。否则,将所有点的度数除以2,得到每个点关联的白边数目和黑边数目,此为剪枝。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int t,n,m,a[],b[],d[];
int B[],W[],ans;
void dfs(int k){
if(k==m){
ans++;return;
}
int u=a[k],v=b[k];
if(B[u]<d[u]&&B[v]<d[v]){
B[u]++,B[v]++;
dfs(k+);
B[u]--,B[v]--;
}
if(W[u]<d[u]&&W[v]<d[v]){
W[u]++,W[v]++;
dfs(k+);
W[u]--,W[v]--;
}
}
int main(){
scanf("%d",&t);
while(t--){
bool flag=true;
scanf("%d%d",&n,&m);
memset(d,,sizeof(d));
for(int i=;i<m;i++){
scanf("%d%d",&a[i],&b[i]);
d[a[i]]++;d[b[i]]++;
}
for(int i=;i<=n;i++){
if(d[i]&)
flag=false;
d[i]/=;
}
if(!flag){
puts("");
continue;
}
memset(W,,sizeof(W));
memset(B,,sizeof(B));
ans=;
dfs();
printf("%d\n",ans);
}
return ;
}