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. 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)

    1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置  1.命令行参数 所有的配置都可以在命令行上进行指定 ...

  2. Eclipse安装Sonarlint插件

    这里安装的是Sonarlint3.6.插件安装非常简单.插件比Sonar更为简单快捷. 一.首先通过点击Eclipse上方Help菜单会出现一个下拉列表,点击其中的Eclipse MarketPlac ...

  3. unittest单元测试框架之测试环境的初始化与还原(fixture)(五)

    1.方法一:针对每条测试用例进行初始化与还原 import unittest from UnittestDemo.mathfunc import * class TestMathFunc(unitte ...

  4. Oracle 11gR2 RAC 11.2.0.4 全自动打11.2.0.4.20170418 psu 步骤

    说明:此次环境的patch p6880880_112000_Linux-x86-64.zip 和 p25476126_112040_Linux-x86-64.zip均放在/soft目录, 且grid和 ...

  5. forEach for...in for...of

    forEach orEach 方法为数组中含有有效值的每一项执行一次 callback 函数,那些已删除(使用 delete 方法等情况)或者从未赋值的项将被跳过(不包括那些值为 undefined ...

  6. stack permutation

    #include <iostream> #include <stack> #include <queue> using namespace std; bool ch ...

  7. About Me - 关于

    0x00 简介 97年生 计算机相关专业,无线电安全攻防方向. 涉猎较多,喜欢研究无线.硬件.软件.网络.攻击.检测.防御等各类安全技术 精通较少,主要擅长的还是硬件.渗透.无线攻击方面. 现阶段在研 ...

  8. SQL语句中生成UUID方法

    SQL语句中生成UUID方法为UUID() 生成带横线UUID: select UUID()                         形如:abaffaca-fd55-11e5-b3d0-d2 ...

  9. css 自定义checkbox多选复选框样式

    html: <input type="checkbox" id="" value="">菜单1 <input type=& ...

  10. jQuery 动画效果 与 动画队列

    基础效果 .hide([duration ] [,easing ] [,complete ]) 用于隐藏元素,没有参数的时候等同于直接设置 display 属性 $('.target').hide() ...