hdu 5305 Friends(dfs)

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 (1n8) and m (0mn(n1)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 xy 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
0
2
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 int ans,n,m;
 7 int v[10],v1[10],v2[10];
 8 struct p
 9 {
10    int u,v;
11 };p s[60];
12 void dfs(int i)
13 {
14      if (i==m+1)
15      {
16          ans++;
17          return ;
18      }
19      if (v1[s[i].u]&&v1[s[i].v])
20      {
21           v1[s[i].u]--;
22           v1[s[i].v]--;
23           dfs(i+1);
24           v1[s[i].u]++;
25           v1[s[i].v]++;
26      }
27      if (v2[s[i].u]&&v2[s[i].v])
28      {
29           v2[s[i].u]--;
30           v2[s[i].v]--;
31           dfs(i+1);
32           v2[s[i].u]++;
33           v2[s[i].v]++;
34      }
35 }
36 int main()
37 {
38     int i,j,flag,t;
39     scanf("%d",&t);
40     while (t--)
41     {
42         scanf("%d%d",&n,&m);
43         flag=0;
44         memset(v,0,sizeof(v));
45         memset(v1,0,sizeof(v1));
46         memset(v2,0,sizeof(v2));
47         for (i=1;i<=m;i++)
48         {
49            scanf("%d%d",&s[i].u,&s[i].v);
50            v[s[i].u]++;
51            v[s[i].v]++;
52         }
53         for (i=1;i<=n;i++)
54         {
55            if (v[i]%2==1)
56            {
57               flag=1;
58               break;
59            }
60            v1[i]=v2[i]=v[i]/2;
61         }
62         ans=0;
63         dfs(1);
64         printf("%d
",ans);
65     }
66 }
原文地址:https://www.cnblogs.com/pblr/p/4810230.html