原题链接

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
 
Recommend
wange2014
 
题意:输入n,m,n表示有n个人,m表示m对朋友关系,现在要使每个人的朋友划分为在线朋友和离线朋友,且在线朋友和离线朋友数量相等(一对朋友之间只能是在线朋友或者离线朋友),求方案数;
 
思路:用dfs深搜枚举每一条边(即每一对朋友关系),若能深搜进行完最后一条边,即当前边cnt==m+1  则ans++;
 
代码如下:
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,cnt,ans;
int c1[],c2[],d[];
struct Node
{
int u,v;
}node[]; void dfs(int i)
{
if(i-==m)
{
ans++;
return ;
}
if(c1[node[i].u]&&c1[node[i].v])
{
c1[node[i].u]--;
c1[node[i].v]--;
dfs(i+);
c1[node[i].u]++;
c1[node[i].v]++;
}
if(c2[node[i].u]&&c2[node[i].v])
{
c2[node[i].u]--;
c2[node[i].v]--;
dfs(i+);
c2[node[i].u]++;
c2[node[i].v]++;
}
} int main()
{
int T;
cin>>T;
while(T--)
{
cnt=;
ans=;
scanf("%d%d",&n,&m);
memset(node,,sizeof(node));
memset(c1,,sizeof(c1));
memset(c2,,sizeof(c2));
memset(d,,sizeof(d));
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
node[++cnt].u=u;
node[cnt].v=v;
d[u]++;
d[v]++;
}
int flag=;
for(int i=;i<=n;i++)
{
c1[i]=c2[i]=d[i]/;
if(d[i]&)
{
flag=;
break;
}
}
if(flag)
{
puts("");
continue;
}
dfs();
printf("%d\n",ans);
}
return ;
}
 

最新文章

  1. AngularJS移动开发中的各种坑
  2. Nancy之ModelBinding(模型绑定)
  3. 为IEnumerable扩展一个ForEach方法
  4. 图片上传并显示(兼容ie),图片大小判断
  5. swift学习第五章-字典的使用
  6. vim搭建笔记
  7. pip3更新后install package出现ImportError: cannot import name &#39;main&#39;
  8. 了解Java内存模型,看完这一篇就够了
  9. 小R的树(权限题)
  10. python全栈开发day21-2 几个装饰器总结
  11. C# System.Collections.Queue
  12. vue 定时器的问题
  13. C语言入门:02.第一个C语言程序
  14. 用make编译openCV报错:ts_gtest.cpp:(.text._ZN7testing8internal2RED2Ev+0xf): undefined reference to &#39;regfreeA&#39;
  15. MongoDB和pymongo自用手册
  16. Linux多线程的使用一:互斥锁
  17. C#基础入门 五
  18. lintcode-501-迷你推特
  19. 重构改善既有代码设计--重构手法16:Introduce Foreign Method (引入外加函数)&amp;&amp; 重构手法17:Introduce Local Extension (引入本地扩展)
  20. 【#】Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释

热门文章

  1. Node.js入门:包结构
  2. Java Synchronized Blocks
  3. 查看Wait type
  4. 编译原理:正规式转变成DFA算法
  5. 【原创】.NET读写Excel工具Spire.Xls使用(3)单元格控制
  6. Office英语学习好帮手
  7. Android基于mAppWidget实现手绘地图(十六)–处理一次触摸多个地图对象
  8. Sybase 数据库新增用户,赋权
  9. Wee – 为现代 Web 开发打造的 CSS 脚手架
  10. 优秀前端开发教程:超炫的 Mobile App 3D 演示