2015 Multi-University Training Contest 2 Friends

Friends

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


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 <cmath>
 4 using namespace std;
 5 const int maxn = 50;
 6 struct arc {
 7     int u,v;
 8 } e[maxn];
 9 int st[maxn],du[maxn],n,m,ret;
10 bool check() {
11     for(int i = 1; i <= n; ++i)
12         if(st[i]) return false;
13     return true;
14 }
15 bool check2(int x){
16     if(st[x] == 0 && (du[x]&1) == 0) return true;
17     int tmp = st[x]<0?du[x]+st[x]:du[x]-st[x];
18     if(st[x] < 0 && tmp >= 0 && (tmp&1) == 0) return true;
19     if(st[x] > 0 && tmp >= 0 && (tmp&1) == 0) return true;
20     return false;
21 }
22 void dfs(int cur) {
23     if(cur == m) {
24         if(check()) ++ret;
25         return;
26     }
27     ++st[e[cur].u];
28     ++st[e[cur].v];
29     --du[e[cur].u];
30     --du[e[cur].v];
31     if(check2(e[cur].u) && check2(e[cur].v)) dfs(cur+1);
32     st[e[cur].v] -= 2;
33     st[e[cur].u] -= 2;
34     if(check2(e[cur].u && check2(e[cur].v))) dfs(cur+1);
35     ++st[e[cur].v];
36     ++st[e[cur].u];
37     ++du[e[cur].u];
38     ++du[e[cur].v];
39 }
40 int main() {
41     int kase;
42     scanf("%d",&kase);
43     while(kase--) {
44         scanf("%d%d",&n,&m);
45         memset(du,0,sizeof du);
46         memset(st,0,sizeof st);
47         for(int i = ret = 0; i < m; ++i) {
48             scanf("%d%d",&e[i].u,&e[i].v);
49             ++du[e[i].u];
50             ++du[e[i].v];
51         }
52         bool flag = true;
53         for(int i = 1; i <= n && flag; ++i)
54             if(du[i]&1) flag = false;
55         if(flag) dfs(0);
56         printf("%d
",ret);
57     }
58     return 0;
59 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4671863.html