原题链接

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
 
Author
XJZX
 
Source
 
Recommend
wange2014
 
题意:输入n,m,n表示有n个人,m表示m对朋友关系,现在要使每个人的朋友划分为在线朋友和离线朋友,且在线朋友和离线朋友数量相等(一对朋友之间只能是在线朋友或者离线朋友),求方案数;
 
思路:用dfs深搜枚举每一条边(即每一对朋友关系),若能深搜进行完最后一条边,即当前边cnt==m+1  则ans++;
 
代码如下:
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,cnt,ans;
int c1[],c2[],d[];
struct Node
{
int u,v;
}node[]; void dfs(int i)
{
if(i-==m)
{
ans++;
return ;
}
if(c1[node[i].u]&&c1[node[i].v])
{
c1[node[i].u]--;
c1[node[i].v]--;
dfs(i+);
c1[node[i].u]++;
c1[node[i].v]++;
}
if(c2[node[i].u]&&c2[node[i].v])
{
c2[node[i].u]--;
c2[node[i].v]--;
dfs(i+);
c2[node[i].u]++;
c2[node[i].v]++;
}
} int main()
{
int T;
cin>>T;
while(T--)
{
cnt=;
ans=;
scanf("%d%d",&n,&m);
memset(node,,sizeof(node));
memset(c1,,sizeof(c1));
memset(c2,,sizeof(c2));
memset(d,,sizeof(d));
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
node[++cnt].u=u;
node[cnt].v=v;
d[u]++;
d[v]++;
}
int flag=;
for(int i=;i<=n;i++)
{
c1[i]=c2[i]=d[i]/;
if(d[i]&)
{
flag=;
break;
}
}
if(flag)
{
puts("");
continue;
}
dfs();
printf("%d\n",ans);
}
return ;
}
 

2015暑假多校联合---Friends(dfs枚举)的更多相关文章

  1. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  2. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

  3. 2015暑假多校联合---CRB and His Birthday(01背包)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...

  4. 2015暑假多校联合---Expression(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...

  5. 2015暑假多校联合---Zero Escape(变化的01背包)

    题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...

  6. 2015暑假多校联合---Assignment(优先队列)

    原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...

  7. 2015暑假多校联合---Problem Killer(暴力)

    原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...

  8. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  9. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

随机推荐

  1. weinre使用

    2016-1-21 更新说明: 微信web开发者工具已经集成了weinre,只需设置手机代理便可调试任意页面,更简单更方便,推荐使用! Web应用开发者需要针对手机进行界面的调试,但是手机上并没有称心 ...

  2. atitit。ocr框架类库大全 attilax总结

    atitit.ocr框架类库大全 attilax总结 Tesseract Asprise JavaOCR 闲来无事,发现百度有一个OCR文字识别接口,感觉挺有意思的,拿来研究一下. 百度服务简介:文字 ...

  3. MVC3中如何输出富文本

    MVC3中如何输出富文本 在网站的文本输出中,经常会将DB里的文本输出到页面上. 一般来说是直接利用MVC3中的ViewBag将文本带到前台并表示, 或是是直接以<%:model.data%&g ...

  4. Callable、Future、RunnableFuture、FutureTask的原理及应用

    1. Callable.Future.RunnableFuture.FutureTask的继承关系 在多线程编程中,我们一般通过一个实现了Runnable接口的对象来创建一个线程,这个线程在内部会执行 ...

  5. Android属性动画之ObjectAnimator

    相信对于Android初学者,对于Android中的动画效果一定很感兴趣,今天为大家总结一下刚刚学到的属性动画案例. 首先和一般的Android应用一样,我们先建一个工程,为了方便,我们的布局文件中就 ...

  6. 邻接表有向图(一)之 C语言详解

    本章介绍邻接表有向图.在"图的理论基础"中已经对图进行了理论介绍,这里就不再对图的概念进行重复说明了.和以往一样,本文会先给出C语言的实现:后续再分别给出C++和Java版本的实现 ...

  7. Windows Azure HandBook (7) 基于Azure Web App的企业官网改造

    <Windows Azure Platform 系列文章目录> 1.用户场景: C公司是全球大型跨国连锁餐厅,在世界上大约拥有3万间分店.其IT系统主要部署其海外数据中心,或者租用其他ID ...

  8. plsql导入excel时报错:ORA-01036: 非法变量名/编号

    导入oracle数据,选择工具->odbc导入->Excel 然后关于日期的插入出错,修改后如下:

  9. nodejs学习笔记三——nodejs使用富文本插件ueditor

    在做自己的nodejs项目的时候遇到需要使用ueditor.原来下载的是ueditor的jsp版本.目录如下  在ueditor.config.js中有配置服务器home路径(这个home路径能找到u ...

  10. Tornado框架

    Tornado介绍 Tornado 是 FriendFeed 使用的可扩展的异步非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py(豆瓣用这个写的) 或者 Go ...