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. dropout总结

    1.伯努利分布:伯努利分布亦称“零一分布”.“两点分布”.称随机变量X有伯努利分布, 参数为p(0<p<1),如果它分别以概率p和1-p取1和0为值.EX= p,DX=p(1-p). 2. ...

  2. OC报错,after command failed: Directory not empty

    Directory not empty这个错误经常出现,出现的原因也很多,今天主要记录一下楼主自己碰到的这种情况. 全部错误提示: error: couldn't remove ‘路径/app-fzy ...

  3. torch.backend.cudnn.benchmark

    大部分情况下,设置这个 flag 可以让内置的 cuDNN 的 auto-tuner 自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题. 一般来讲,应该遵循以下准则: 如果网络的输入数据维度 ...

  4. 应对STM32 Cortex-M3 Hard Fault异常

    STM32 Cortex-M3 Hard Fault Hard fault (硬错误,也有译为硬件错误的)是在STM32(如无特别说明,这里的STM32指的是Cortex-M3的核)上编写程序中所产生 ...

  5. Java Bean与Map之间相互转化的实现

    目录树 概述 Apache BeanUtils将Bean转Map Apache BeanUtils将Map转Bean 理解BeanUtils将Bean转Map的实现之手写Bean转Map 概述 Apa ...

  6. 【转载】RETE算法研究

    本文转自:http://www.ibm.com/developerworks/cn/opensource/os-drools/ RETE算法是大多数规则引擎采用的一种模式匹配算法,比如开源的Drool ...

  7. 编译问题: "ld: duplicate symbol _OBJC_METACLASS_$_XXX..."

    在新的SDK环境中调试百度地图的应用程序时,app总是意外退出,找了半天发现错误的原因是unrecognized selector xx的错误,另外还有报了一个Unknown class XXX in ...

  8. Bootstrap Data Table简单使用(动态加载数据)

    直接上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  9. Windows10:Opencv4.0+Opencv4.0.1_contrib编译

    操作系统:windows10 64bit 已安装工具:VS2017 64bit,cmake3.12bit. 安装Cmake:到cmake下载3.12及以上版本,64bit, 选择windows下的安装 ...

  10. kali linux 安装TIM or QQ(CrossOver 安装 QQ)

    需要文件 http://www.crossoverchina.com/xiazai.html dpkg --add-architecture i386 apt-get update apt-get i ...