原题链接

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. jQuery实现全选、全不选、反选

    如图,需要使用jQuery实现全选.全不选.反选功能: 核心代码: 全选 $("#check_all").click(function(){ $("input:check ...

  2. Nodejs·网络服务

    本章是从NodeJS拥有的模块角度,讲述了网络服务中的应用: net ----- > TCP dgram --> UDP http -----> HTTP https ----> ...

  3. Elasticsearch推荐插件篇(head,sense,marvel)

    安装head head插件可以用来快速查看elasticsearch中的数据概况以及非全量的数据,也支持控件化查询和rest请求,但是体验都不是很好. 一般就用它来看各个索引的数据量以及分片的状态. ...

  4. Atitit 异常机制与异常处理的原理与概论

    Atitit 异常机制与异常处理的原理与概论 1. 异常vs 返回码1 1.1. 返回码模式的处理 (瀑布if 跳到失败1 1.2. 终止模式  vs 恢复模式(asp2 1.3. 异常机制的设计原理 ...

  5. Atitit main函数的ast分析  数组参数调用的ast astview解析

    Atitit main函数的ast分析  数组参数调用的ast astview解析 1.1. Xxcls.main(new String[]{"","bb"}) ...

  6. mybatis插入的同时获取主键id

    <insert id="insertAndReturnId" parameterType="com.qianlong.cms.entity.AppCmsRole&q ...

  7. nginx上部署python web

    nginx上部署python web http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

  8. React(一)基础点

    prop实例 <div id="app"></div> <script src="bower_components/react/react. ...

  9. 【.NET深呼吸】INotifyPropertyChanged接口的真故事

    无论是在流氓腾的问问社区,还是在黑度贴吧,或是“厕所等你”论坛上,曾经看到过不少朋友讨论INotifyPropertyChanged接口.不少朋友认为该接口是为双向绑定而使用的,那么,真实的情况是这样 ...

  10. elastic-job

    github源码: https://github.com/dangdangdotcom/elastic-job maven中央仓: http://repo1.maven.org/maven2/com/ ...