Friends
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1429 Accepted Submission(s): 725
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
0 2
Author
XJZX
Source
2015 Multi-University Training Contest 2
#include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef pair<int , int > P; #define maxn 10 int num[maxn], g1[maxn], g2[maxn], n, m, T; P e[30]; int sum; void dfs(int t) { if(t == m + 1) { sum++; return ; } int u = e[t].first; int v = e[t].second; if(g1[u] && g1[v])// online { g1[u]--, g1[v]--; dfs(t + 1); g1[u]++, g1[v]++; } if(g2[u] && g2[v])// offline { g2[u]--, g2[v]--; dfs(t + 1); g2[u]++, g2[v]++; } return ; } int main() { scanf("%d", &T); while(T--) { memset(num, 0, sizeof num); memset(g1, 0, sizeof g1); memset(g2, 0, sizeof g2); int flag = 1; scanf("%d%d", &n, &m); int u, v; for(int i=1; i<=m; i++) { scanf("%d%d", &u, &v); e[i] = {u, v}; num[u]++; num[v]++; } for(int i=1; i<=n; i++) { if(num[i] & 1) { flag = 0; break; } num[i] >>= 1; g1[i] = g2[i] = num[i]; } sum = 0; dfs(1); if(flag) printf("%d\n", sum); else printf("0\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。