Friends

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5305

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

Hint

题意

给你一个图,n点m边,你现在边分为两种,叫做“线上朋友”和“线下朋友”

现在对于每个人要求线上朋友和线下朋友一样多

问你有多少种方案。

题解:

数据范围太小了,直接暴力dfs就好了……

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 115;
int n,m,ans,l[maxn],r[maxn];
vector<int>E[maxn];
int cnt1[maxn],cnt2[maxn],cnt[maxn];
void init()
{for(int i=0;i<maxn;i++)E[i].clear();memset(cnt,0,sizeof(cnt));memset(cnt1,0,sizeof(cnt1));memset(cnt2,0,sizeof(cnt2));ans=0;
}
void dfs(int x)
{if(x==m){int flag = 0;for(int i=1;i<=n;i++){if(cnt1[i]!=cnt2[i]){flag=1;break;}}if(!flag)ans++;return;}if(cnt1[l[x]]<cnt[l[x]]/2&&cnt1[r[x]]<cnt[r[x]]/2){cnt1[l[x]]++;cnt1[r[x]]++;dfs(x+1);cnt1[l[x]]--;cnt1[r[x]]--;}if(cnt2[l[x]]<cnt[l[x]]/2&&cnt2[r[x]]<cnt[r[x]]/2){cnt2[l[x]]++;cnt2[r[x]]++;dfs(x+1);cnt2[l[x]]--;cnt2[r[x]]--;}
}
void solve()
{init();scanf("%d%d",&n,&m);for(int i=0;i<m;i++){int x,y;scanf("%d%d",&x,&y);E[x].push_back(y);E[y].push_back(x);l[i]=x,r[i]=y;cnt[x]++,cnt[y]++;}for(int i=1;i<=n;i++){if(E[i].size()%2==1){cout<<"0"<<endl;return;}}dfs(0);cout<<ans<<endl;
}
int main()
{int t;scanf("%d",&t);while(t--)solve();return 0;
}

HDU 5305 Friends dfs相关推荐

  1. hdu 5692 Snacks(dfs序+线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692 解题思路:这道题是树节点的点权更新,而且涉及到子树,常用的思路是利用dfs序,用线段树来对区间进 ...

  2. hdu 5424(dfs搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5424 官方题解: 如果图是联通的,可以发现如果存在哈密顿路径,一定有一条哈密顿路径的一端是度数最小的点 ...

  3. hdu 4714 树+DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 本来想直接求树的直径,再得出答案,后来发现是错的. 思路:任选一个点进行DFS,对于一棵以点u为 ...

  4. hdu 1241Oil Deposits(dfs模板)

    题目链接-- http://acm.hdu.edu.cn/showproblem.php?pid=1241 首先给出一个n*m的字符矩阵,'*'表示空地,'@'表示油井.问在这个矩阵中有多少组油井区? ...

  5. HDU 1240 Asteroids!(DFS简单搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1240 Asteroids! Time Limit: 2000/1000 MS (Java/Others ...

  6. D - Age of Moyu HDU - 6386 -BFS+DFS分层最短路

    D - Age of Moyu HDU - 6386 题意:双向图,m条边,每条边有一个编号id,而花费就是根据这个id的变化来确定的求1-n的最短路 思路:全值为0,1的图直接bfs按层遍历图找到n ...

  7. hdu 4886(hash + dfs)

    TIANKENG's restaurant(Ⅱ) Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 130107/65536 K (Ja ...

  8. hdu 1254(dfs+bfs+优先队列)

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Desc ...

  9. hdu 3681(bfs+dfs+状态压缩)

    解题思路:这道题属于图上来回走的问题,可以把重复走的过程弱化,即只强调从u->v的结果,中间经过的节点都不考虑.这道题里面'G','F','Y'是重要的节点,其余的点我们是可以忽略的,也就是说, ...

最新文章

  1. PaddlePaddle 中的若干基础命令中的问题
  2. VS2010常用快捷键自定义
  3. Django:ORM模型类,字段选项,Meta内部类,常见问题处理
  4. 阿里云:Table Store(OTS) Writer
  5. cython 安装升级_Anaconda安装pytorch,torchvision和 cython
  6. jmeter mysql数据库_jmeter连接mysql数据库
  7. json文件_ajax
  8. 重磅丨三年沉淀 2018全球人工智能技术大会蓄势待发
  9. java参数化查询_【转】参数化查询为什么能够防止SQL注入
  10. 无盘服务器pnp,锐起无盘PNP有哪些配置类型
  11. 数字化测图是利用计算机自动绘制地形图,数字化测图详解
  12. uni-app项目的启动
  13. Java文件传输带宽控制_如何限制Java中的带宽?
  14. 计算机的处理器怎么看,小编教你电脑处理器怎么查看
  15. Artificial Neural Networks FileStorage of OpenCV
  16. 扫描NFC卡,获取卡号存储到本地并可以以Excel形式导出数据
  17. 二叉树的父子节点位置关系
  18. 亚马逊推出的「距离助手」,好像孙悟空给唐僧画的圈圈啊
  19. C语言中变量的作用域
  20. java 线图_在Java中绘制一个简单的线图

热门文章

  1. SCPPO(八):登录实现逻辑
  2. 机房收费系统合作版(一):开始团队合作之旅
  3. 苹果Siri管理层再震荡,7年老领导出局,谷歌老兵治下要重视长期研究
  4. AI主播首次上岗CCTV,撒贝宁有了个虚拟孪生兄弟“小小撒”
  5. 中关村十大AI研究院 | 盘点
  6. JavaScript 对象总结
  7. 转换GridView的内容到Excel里面 ---带有格式
  8. mysql高可用方案之主从架构(master-slave)
  9. JQUERY对RADIO,CHECKBOX,SELECT的操作常用方法大全
  10. gssapiauthentication