Friends
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1797 Accepted Submission(s): 903
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.
2 3 3 1 2 2 3 3 1 4 4 1 2 2 3 3 4 4 1
0 2
n个人中有m对朋友关系,问你有多少种方案可以使得online friends 和 offline friends数量相同。
朋友关系数是奇数时没有方法满足题意,而后每个人进行dfs。
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
bool map[10][10];
int deg[10], c1[10], c2[10];
int n, m, cnt, ans;
struct node
{
/* data */
int u, v;
}e[100005];
void dfs(int x)
{
if(x == m + 1) {
ans++;
return ;
}
int v = e[x].v, u = e[x].u;
if(c1[u] && c1[v]) {
c1[u]--;
c1[v]--;
dfs(x + 1);
c1[u]++;
c1[v]++;
}
if(c2[u] && c2[v]) {
c2[u]--;
c2[v]--;
dfs(x + 1);
c2[u]++;
c2[v]++;
}
return ;
}
int main(int argc, char const *argv[])
{
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
cnt = 0;
ans = 0;
memset(deg, 0, sizeof(deg));
memset(e, 0, sizeof(e));
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));
for(int i = 1; i <= m; ++i) {
scanf("%d%d", &e[i].u, &e[i].v);
deg[e[i].u]++;
deg[e[i].v]++;
}
bool flag = false;
for(int i = 1; i <= n; ++i) {
c1[i] = c2[i] = deg[i] / 2;
if(deg[i] & 1) {
flag = true;
break;
}
}
if(flag) {
printf("0\n");
continue;
}
dfs(1);
printf("%d\n", ans);
}
return 0;
}
8495



被折叠的 条评论
为什么被折叠?



