hdu 5305 Friends(dfs)
2015-09-15 14:36
225 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each testcase, print one number indicating the answer.
[align=left]Sample Input[/align]
2
3 3
1 2
2 3
3 1
4 4
1 2
2 3
3 4
4 1
[align=left]Sample Output[/align]
0
2
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each testcase, print one number indicating the answer.
[align=left]Sample Input[/align]
2
3 3
1 2
2 3
3 1
4 4
1 2
2 3
3 4
4 1
[align=left]Sample Output[/align]
0
2
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int ans,n,m; int v[10],v1[10],v2[10]; struct p { int u,v; };p s[60]; void dfs(int i) { if (i==m+1) { ans++; return ; } if (v1[s[i].u]&&v1[s[i].v]) { v1[s[i].u]--; v1[s[i].v]--; dfs(i+1); v1[s[i].u]++; v1[s[i].v]++; } if (v2[s[i].u]&&v2[s[i].v]) { v2[s[i].u]--; v2[s[i].v]--; dfs(i+1); v2[s[i].u]++; v2[s[i].v]++; } } int main() { int i,j,flag,t; scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); flag=0; memset(v,0,sizeof(v)); memset(v1,0,sizeof(v1)); memset(v2,0,sizeof(v2)); for (i=1;i<=m;i++) { scanf("%d%d",&s[i].u,&s[i].v); v[s[i].u]++; v[s[i].v]++; } for (i=1;i<=n;i++) { if (v[i]%2==1) { flag=1; break; } v1[i]=v2[i]=v[i]/2; } ans=0; dfs(1); printf("%d\n",ans); } }
相关文章推荐
- VS2010下安装boost库
- .NET基础--if与else的匹配问题
- Struts 2 Hello World Annotation Example
- 使用最新版本的Java EE eclipse构建webservice应用
- LoadRunner如何实现动态链接库(C++)的调用
- 使用jquery.validate.js实现boostrap3的校验和验证
- POJ 3268 Silver Cow Party
- LR数据库访问封装的API使用指导书8
- Scheduling (调度)相关
- 避免缓冲区溢出的方法
- Oracle中in和exists的区别
- c++ 11 thread使用
- .NET应用架构设计—工作单位模式(摆脱程序代码的重要思想,反击DDD)
- typedef和typename关键字
- shell expr的用法
- [7]文本输入框默认不获取焦点
- LR数据库访问封装的API使用指导书7
- python tab补全
- zoj1005
- 理解JavaScript原型