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 5305 Friends (DFS,穷举+剪枝)

    题意: 给定n个人,m对朋友关系,如果对于每个人,只能刚好选择其所有朋友中的一半的人进行聊天(只是我和我的朋友,不是我的朋友和我的朋友),那么有多少种情况?只要一个选择不同,视为不同情况. 思路: 比 ...

  2. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  3. DFS HDU 5305 Friends

    题目传送门 /* 题意:每个点都要有偶数条边,且边染色成相同的两部分,问能有多少种染色方法 DFS+剪枝:按照边数来DFS,每种染色数为该点入度的一半,还有如果点不是偶数边就不DFS 这是别人的DFS ...

  4. HDU 5305 Friends(简单DFS)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  5. HDU 5305 Friends(dfs)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  6. 【HDU 5305】Friends 多校第二场(双向DFS)

    依据题意的话最多32条边,直接暴力的话 2 ^ 32肯定超时了.我们能够分两次搜索时间复杂度降低为 2 * 2  ^ 16 唯一须要注意的就是对眼下状态的哈希处理. 我採用的是 十进制表示法 跑的还是 ...

  7. hdu 5305 Friends(2015多校第二场第6题)记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给你n个人,m条关系,关系可以是online也可以是offline,让你求在保证所有人on ...

  8. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  9. hdu 4499 Cannon dfs

    Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...

随机推荐

  1. 如何把一篇Word文档里的所有换行符去掉?

    编辑-查找,查找框输入 ^13替换框不输入点击全部替换

  2. 大数据系列之Hadoop框架

    Hadoop框架中,有很多优秀的工具,帮助我们解决工作中的问题. Hadoop的位置 从上图可以看出,越往右,实时性越高,越往上,涉及到算法等越多. 越往上,越往右就越火…… Hadoop框架中一些简 ...

  3. Python 库汇总中文版

    这又是一个 Awesome XXX 系列的资源整理,由 vinta 发起和维护.内容包括:Web框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理.文本处理.自然语言处理.机器学习. ...

  4. centos7安装lamp

    一.准备工作 1.   下载并安装CentOS7.2,配置好网络环境,确保centos能上网,可以获取到yum源. centos7.2的网络配置: vim /etc/sysconfig/network ...

  5. Java中Enum的使用

    http://www.cnblogs.com/happyPawpaw/archive/2013/04/09/3009553.html Enum有一个好处,可以使用EnumMap和EnumSet,而En ...

  6. C语言调用正则表达式

    标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一功能,其中最著名的当数Philip Hazel的Perl-Compatible Regular Expression库 ...

  7. scrapy使用PhantomJS和selenium爬取数据

    1.phantomjs 安装 下载:http://phantomjs.org/download.html 解压: tar -jxvf phantomjs--linux-x86_64.tar.bz2 重 ...

  8. csu 1553(RMQ+尺取法)

    1553: Good subsequence Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 794  Solved: 287[Submit][Statu ...

  9. Source Insight 4.0 文件类型、编码格式、tab转空格、tab键自动补全设置。。。

    1.编码格式  -- 在 Options->Preferences->Files 中的最下面,Default enconding 为 UTF-8 2.tab转空格 其他相关设置如下: 以下 ...

  10. gcc编译器参数

    [gcc编译步骤] 1.预处理,生成.i的文件[预处理器cpp] 2.将预处理后的文件转换成汇编语言,生成文件.s[编译器egcs] 3.由汇编变为目标代码(机器代码)生成.o的文件[汇编器as] 4 ...