2015 多校赛 第二场 1006 (hdu 5305)
2015-08-04 20:37
267 查看
[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
题意:给出一幅无向图。依次对边染白色或黑色,使得每个点所关联的白边和黑边数目相同。问有多少种染色方法。
思路:
搜索题。强行暴力搜必定超时。枚举点来搜索也不好写。因此枚举边。
记录每个点的度数,若有点的度数为奇数,直接输出0。否则,将所有点的度数除以2,得到每个点关联的白边数目和黑边数目,此为剪枝。
代码如下:
View Code
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
题意:给出一幅无向图。依次对边染白色或黑色,使得每个点所关联的白边和黑边数目相同。问有多少种染色方法。
思路:
搜索题。强行暴力搜必定超时。枚举点来搜索也不好写。因此枚举边。
记录每个点的度数,若有点的度数为奇数,直接输出0。否则,将所有点的度数除以2,得到每个点关联的白边数目和黑边数目,此为剪枝。
代码如下:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int t,n,m,a[30],b[30],d[10]; int B[30],W[30],ans; void dfs(int k){ if(k==m){ ans++;return; } int u=a[k],v=b[k]; if(B[u]<d[u]&&B[v]<d[v]){ B[u]++,B[v]++; dfs(k+1); B[u]--,B[v]--; } if(W[u]<d[u]&&W[v]<d[v]){ W[u]++,W[v]++; dfs(k+1); W[u]--,W[v]--; } } int main(){ scanf("%d",&t); while(t--){ bool flag=true; scanf("%d%d",&n,&m); memset(d,0,sizeof(d)); for(int i=0;i<m;i++){ scanf("%d%d",&a[i],&b[i]); d[a[i]]++;d[b[i]]++; } for(int i=1;i<=n;i++){ if(d[i]&1) flag=false; d[i]/=2; } if(!flag){ puts("0"); continue; } memset(W,0,sizeof(W)); memset(B,0,sizeof(B)); ans=0; dfs(0); printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- AngularJS学习笔记
- 搜索 - hdu5288 OO’s Sequence
- eclipse show in windows explorer
- 最简单通用加载效果---完全实现自定义
- 解决对表空间没有权限的问题
- Codeforces 4B
- JSP中页面跳转response.sendRedirect()和request.getRequestDispatcher()的区别
- 【effective c++读书笔记】【第5章】实现(1)
- java Timer(定时调用、实现固定时间执行)
- hdu 5351 MZL's Border 打表+高精度
- 【effective c++读书笔记】【第5章】实现(1)
- 项目分析(2)
- 手势识别器(UIImageView)
- HDU - 4360As long as Binbin loves Sangsang最短路问题多状态记录
- uva 101
- zoj题目分类详细的
- thread safe lazy initialization singleton
- 啊哈,算法! -----dfs1
- Spring--Spring容器
- [MetaHook] R_SparkShower