Friends

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2945    Accepted Submission(s): 1413

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

有n个人和m对朋友关系,他们中间有关系好的,也有关系不好的。要使他们每个人的好朋友和一般朋友数量相等(比如一个人有2个好朋友,那么他就应该也有2个一般朋友),有多少种方案。

输入的时候就可以保存每个人的朋友总数,如果有一个人的朋友总数是奇数,那么就不可能达到题目要求的条件,直接输出0;

不然的话,就另外用2个数组保存每个人的好朋友数量和一般朋友数量,都是朋友总数的一半。

接下来就是dfs,先找好朋友关系,如果两个人中有一个人的好朋友关系用完了,那就只能看这两个人的坏朋友关系,如果其中有一个人的坏朋友关系也用完了,那就return;像第一个案例就是没到p==m,就return了。不然就继续找下去;能到p==m就表示所有组都己经找完了,而且也肯定是一半好朋友,一半坏朋友,不然的话中间就return了,不会再往下找(看了好久才明白emmmm)

http://www.voidcn.com/article/p-eifhpyyl-bhn.html

 #include<bits/stdc++.h>
using namespace std;
int n,m;
int num[];
int pa[],pb[];
int sum;
struct node
{
int x;
int y;
}q[]; void DFS(int p)
{
//printf("p = %d\n",p);
if(p == m)
{
sum++;
return ;
}
int x = q[p].x;
int y = q[p].y;
if(pa[x] && pa[y])//x和y的好朋友次数都还有
{
pa[x]--;
pa[y]--;
// cout<<"好朋友"<<x<<" "<<y<<endl;
DFS(p+);
pa[x]++;
pa[y]++;
}
if(pb[x] && pb[y])//x和y的坏朋友次数都还有
{
pb[x]--;
pb[y]--;
// cout<<"坏朋友"<<x<<" "<<y<<endl;
DFS(p+);
pb[x]++;
pb[y]++;
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
sum = ;
memset(num,,sizeof(num));
memset(pa,,sizeof(pa));
memset(pb,,sizeof(pb));
scanf("%d%d",&n,&m);
int x,y;
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
q[i].x = x;
q[i].y = y;
num[x]++;//x的朋友数量加1
num[y]++;
}
int flag = ;
for(int i=;i<=n;i++)
{
pa[i] = num[i]/;//i的好朋友是i的朋友总数的一半
pb[i] = num[i]/;//一般朋友
if(num[i]% == )//因为要每个人的朋友好和一般朋友相等,所以如果出现奇数肯定不行
{
flag = ;
break;
}
}
// cout<<endl;
if(flag)
{
printf("0\n");
continue;
}
DFS();
printf("%d\n",sum);
}
return ;
}

hdu5305 Friends(dfs,多校题)的更多相关文章

  1. HDU 5399 Too Simple(过程中略微用了一下dfs)——多校练习9

    Too Simple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Probl ...

  2. 图的遍历 | 1131地铁图: dfs复杂模拟题

    这题在搞清楚思路绕过坑后,还是可以写的出通过sample data的代码的.但是不能AC,让我很气. 最后查清原因:还是对dfs本质理解的不够. wa代码: vis[s]=1; dfs(s,e,0); ...

  3. BZOJ1306 [CQOI2009]match循环赛/BZOJ3139 [Hnoi2013]比赛[dfs剪枝+细节题]

    地址 看数据范围很明显的搜索题,暴力dfs是枚举按顺序每一场比赛的胜败情况到底,合法就累计.$O(3^{n*(n-1)/2})$.n到10的时候比较大,考虑剪枝. 本人比较菜所以关键性的剪枝没想出来, ...

  4. POJ 2023 Choose Your Own Adventure(树形,dfs,简单题)

    题意: 输入一个整数n,表示有n组测试数据, 每组第一行输入一个整数x表示该组测试一共有x页,接下来输入x行,每行表示一页, 每页或者以C开头(第一页都是以C开头),或者以E开头,中间用引号括起一段文 ...

  5. 树链剖分||dfs序 各种题

    1.[bzoj4034][HAOI2015]T2 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把 ...

  6. hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)

    Problem Description There was no donkey ,) , the down-right cell ,N-) and the cell below the up-left ...

  7. POJ 1321 棋盘问题(非常经典的dfs,入门题)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66277   Accepted: 31639 Descriptio ...

  8. hdu6062RXD and logic gates多校题 构造

    听说标算的点数是2^(n+1)级别的,也不知道我是不是比标算优一点? (话说这种题一眼看过去怎么跟题答一样) 然而并不是题答,没法手玩,来考虑一下一般解法: 考虑一个规模较小的问题:最后一位一定是0 ...

  9. hdu5305 Friends(dfs+map/hash)

    题目:pid=5305">http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给定N个人和M条朋友关系,是朋友关系的两个人之间有两种联系 ...

随机推荐

  1. thinkPHP输出sql语句(3.2和5.0通用)

    //5.0$qwe = db::table('think_user')->where('id',1)->fetchsql()->column('name'); dump($qwe); ...

  2. Mac python3.5 + Selenium 开发环境配置

    一. python 3.5 1. 下载 2. Mac默认为2.7,所以这里主要介绍如何将系统Python默认修改为3.5. 原理: 1)Mac自带的python环境在: python2.7: /Sys ...

  3. 二十三、详述 IntelliJ IDEA 中恢复代码的方法「进阶篇」

    咱们已经了解了如何将代码恢复至某一版本,但是通过Local History恢复代码有的时候并不方便,例如咱们将项目中的代码进行了多处修改,这时通过Local History恢复代码就显得很麻烦,因为它 ...

  4. Nodejs与mysql连接池的应用(pool)

    /* * 连接池 连接和缓存的技术 * */ var mysql = require('mysql'); var pool = mysql.createPool({ connectionLimit:2 ...

  5. NopCommerce 3.4省市联动

    做法有两种,一种是在StateProvince表里面加个字段,另一种是新建两个表,用来存市.县的数据,表结构完全按照StateProvince走就好了.我这里用的是第二种做法,菜鸟一枚,代码写的比较烂 ...

  6. careercup-扩展性和存储限制10.4

    题目 有一个数组,里面的数在1到N之间,N最大为32000.数组中可能有重复的元素(即有的元素 存在2份),你并不知道N是多少.给你4KB的内存,你怎么把数组中重复的元素打印出来. 解答 我们有4KB ...

  7. NodeJ node.js Koa2 跨域请求

    Koa2 .3 跨域请求 Haisen's  需求分析 (localhost:8080 = 前端  [请求]  localhost:8081 = 服务器 ) 1.一个前台    一个服务器    前台 ...

  8. 正则表达式-Regular expression学习笔记

    正则表达式 正则表达式(Regular expression)是一种符号表示法,被用来识别文本模式. 最近在学习正则表达式,今天整理一下其中的一些知识点 grep - 打印匹配行 grep 是个很强大 ...

  9. 09.安装Collabora Online服务

    安装Collabora Online服务 参考博客:http://blog.sina.com.cn/s/blog_16b158be80102xp5u.html 一.安装docker服务 yum ins ...

  10. ThinkPHP5.0框架事务处理操作简单示例

    本文介绍ThinkPHP5.0框架事务处理操作,结合实例形式分析了ThinkPHP5针对删除操作的事务处理相关操作技巧,可以加深对ThinkPHP源码的理解,需要的朋友可以参考下 事务的调用在mysq ...