杭电5305 Friends 搜索
来源:互联网 发布:算法竞赛宝典 百科 编辑:程序博客网 时间:2024/05/29 16:49
题意:有n个人(n<=8),每个人有一定数量的朋友,而和朋友的关系是线上朋友或者线下朋友(只能是其中一种)。问每个人的线上朋友数量都等于线下朋友数量的方法数有多少。
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <iostream> #include <queue> #include <algorithm> #include <vector> using namespace std; #define LL __int64 int f[10],on[10],off[10]; int m,n; struct node { int x,y; }nn[30]; void init() { memset(f,0,sizeof(f)); memset(on,0,sizeof(on)); memset(off,0,sizeof(off)); } int dfs(int cot) { int x,y,i,ans; ans=0; if(cot>=m) { for(i=1;i<=n;i++) { if(on[i]!=off[i]) return 0; } return 1; } x=nn[cot].x; y=nn[cot].y; if(on[x]<f[x]/2&&on[y]<f[y]/2) { on[x]++; on[y]++; ans+=dfs(cot+1); on[x]--; on[y]--; } if(off[x]<f[x]/2&&off[y]<f[y]/2) { off[x]++; off[y]++; ans+=dfs(cot+1); off[x]--; off[y]--; } return ans; } int main() { int i,T,ans; while(~scanf("%d",&T)) { while(T--) { scanf("%d%d",&n,&m); init(); for(i=0;i<m;i++) { scanf("%d%d",&nn[i].x,&nn[i].y); f[nn[i].x]++; f[nn[i].y]++; } if(m&1) //关系数为奇数 //It is guaranteed that x≠y and every friend relationship will appear at most once. { printf("0\n"); continue; } int flag=0; for(i=1;i<=n;i++) { if(f[i]&1) { flag=1; break; } } if(flag) { printf("0\n"); continue; } ans=dfs(0); printf("%d\n",ans); } } return 0; }
0 0
- 杭电5305 Friends 搜索
- hdu 5305 Friends(搜索+剪枝)
- 暴力搜索(HDU 5305,Friends)
- 杭电1312-搜索
- 杭电1195 搜索
- 杭电 3172 Virtual Friends【并查集 + map容器】
- 杭电1312,搜索题
- 杭电1241搜索题
- 杭电1239—搜索
- 杭电ACM_1010_搜索题
- 杭电ACM1238(搜索题)
- 杭电ACM1239(搜索题)
- 杭电ACM1010(搜索题)
- 杭电ACM1241(搜索题)
- 杭电 二叉搜索树
- 杭电 3791 搜索二叉树
- 杭电1016深度搜索问题
- 杭电ACM 1010 搜索题
- solr5.5+tomcat8+jdk1.8配置
- AtCoder:AtCoder Group Contest(思维)
- mysql非法关机导致 错误 1067:进程意外终止
- Find a multiple (POJ
- Median of Two Sorted Arrays
- 杭电5305 Friends 搜索
- c++之A a和A *a=new A()
- LeetCode (Insert Interval)
- 归并排序
- 常用的创建线程池的方法
- jQuery语法,是一种很接近于java的语法,即 对象.function()
- 使用fastdfs_client的storage.upload_file方法上传图片抛FileNotFound异常
- poj 1061 扩展欧几里德
- 推荐系统概述3