Problem 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

题意:给出一幅无向图。依次对边染白色或黑色,使得每个点所关联的白边和黑边数目相同。问有多少种染色方法。

思路:

搜索题。强行暴力搜必定超时。枚举点来搜索也不好写。因此枚举边。

记录每个点的度数,若有点的度数为奇数,直接输出0。否则,将所有点的度数除以2,得到每个点关联的白边数目和黑边数目,此为剪枝。

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int t,n,m,a[],b[],d[];
int B[],W[],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+);
B[u]--,B[v]--;
}
if(W[u]<d[u]&&W[v]<d[v]){
W[u]++,W[v]++;
dfs(k+);
W[u]--,W[v]--;
}
}
int main(){
scanf("%d",&t);
while(t--){
bool flag=true;
scanf("%d%d",&n,&m);
memset(d,,sizeof(d));
for(int i=;i<m;i++){
scanf("%d%d",&a[i],&b[i]);
d[a[i]]++;d[b[i]]++;
}
for(int i=;i<=n;i++){
if(d[i]&)
flag=false;
d[i]/=;
}
if(!flag){
puts("");
continue;
}
memset(W,,sizeof(W));
memset(B,,sizeof(B));
ans=;
dfs();
printf("%d\n",ans);
}
return ;
}

2015 多校赛 第二场 1006 (hdu 5305)的更多相关文章

  1. 2015 多校赛 第二场 1004 hdu(5303)

    Problem Description There are n apple trees planted along a cyclic road, which is L metres long. You ...

  2. 2015 多校赛 第二场 1002 (hdu 5301)

    Description Your current task is to make a ground plan for a residential building located in HZXJHS. ...

  3. 2015 多校赛 第一场 1007 (hdu 5294)

    总算今天静下心来学算法.. Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the e ...

  4. 2015 多校赛 第一场 1002 (hdu 5289)

    Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n ...

  5. 2015 多校赛 第一场 1001 (hdu 5288)

    Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l&l ...

  6. 2019HDU多校赛第二场 H HDU 6598 Harmonious Army(最小割模型)

    参考博客https://blog.csdn.net/u013534123/article/details/97142191 #include<bits/stdc++.h> using na ...

  7. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

  8. SCNU省选校赛第二场B题题解

    今晚的校赛又告一段落啦,终于"开斋"了! AC了两题,还算是满意的,英语还是硬伤. 来看题目吧! B. Array time limit per test 2 seconds me ...

  9. HDU 5305 Friends (搜索+剪枝) 2015多校联合第二场

    開始对点搜索,直接写乱了.想了想对边搜索,尽管复杂度高.剪枝一下水过去了. 代码: #include<cstdio> #include<iostream> #include&l ...

随机推荐

  1. IOS内购--后台PHP认证

    参考网址:https://blog.csdn.net/que_csdn/article/details/80861408 http://www.php.cn/php-weizijiaocheng-39 ...

  2. SGU100

    Read integers A and B from input file and write their sum in output file. Input Input file contains ...

  3. enote笔记语言(2)(ver0.2)

    why not(whyn't)                     为什么不(与“why”相对应,是它的反面)   how对策 how设计   key-memo                   ...

  4. JAVA学习总结-基础语法

    /** * 这篇文章供自己学习JAVA总结回顾使用 * 主要借鉴了马士兵老师的视频进行总结 * @author Kingram */ 标识符的概念和命名规则 JAVA常量---不可变的变量 程序的执行 ...

  5. hdu 5176 The Experience of Love

    The Experience of Love  Accepts: 11  Submissions: 108  Time Limit: 4000/2000 MS (Java/Others)  Memor ...

  6. Flask-SQLAlchemy中解决1366报错

    会报下面这个1366的错 安装MySQL驱动(我升级过Python,所以要再装一遍) 本想安装MySQL官方驱动mysql-connector-python的,然而官方目前只支持到3.4 我又懒,所以 ...

  7. Huawei-R&S-网络工程师实验笔记20190524-VRP的系统、接口视图下基本操作

    >Huawei-R&S-网络工程师实验笔记20190524-VRP的系统.接口视图下基本操作(重命名.配置IP.VLAN接口.双工模式.console口) >>实验开始,先上 ...

  8. 用Twebbrowser做可控编辑器与MSHTML

    首先要明白mshtml的属性方法: {IHTMLDocument2 方法:} write //写入 writeln //写入并换行 open //打开一个流,以收集 document.write 或 ...

  9. RaspberryPi3安装CentOS7教程

    1.准备 Centos 7 AMR版镜像下载地址: http://mirror.centos.org/altarch/7/isos/armhfp/ 下载得到:CentOS-Userland-7-arm ...

  10. git 拉取远程分支 --本地分支不存在

    git checkout -b 本地分支名 origin/远程分支名