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. tp3.2中前台模板中日期时间的转换

    {$vo.create_time|date='Y-m-d',###} 其中###是占位符

  2. Linux.开关机&登出&用户管理

    关机重启: shutdown:         shutdown –h now 立该进行关机         shudown -h 1 "hello, 1 分钟后会关机了"    ...

  3. mac 系统安装selenium注意事项

    mac最新系统:OS X EI Captian python: 本机自带的python2.7. (本来想升级3.5,觉得太复杂,放弃了) pip: https://pypi.python.org/py ...

  4. Redis(RedisTemplate)运算、算法(incr、decr、increment)

    RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html package com.wbg.springRedis.test ...

  5. “SAP.Middleware.Connector.RfcDestinationManager”的类型初始值设定项引发异常

    在VS2015中使用SAP Connector 3.0(SapNco)的.net4.0x86版本开发时,程序运行到RfcDestinationManager.TryGetDestination时报错: ...

  6. Spring Bean自动注册的实现方案

    这里Spring管理的Bean,可以认为是一个个的Service,每个Service都是一个服务接口 自动注册Service的好处: 1.根据指定的name/id获取对应的Service,实现简单工厂 ...

  7. linux内存管理---虚拟地址、逻辑地址、线性地址、物理地址的区别(一)

    分析linux内存管理机制,离不了上述几个概念,在介绍上述几个概念之前,先从<深入理解linux内核>这本书中摘抄几段关于上述名词的解释: 一.<深入理解linux内核>的解释 ...

  8. TCP/IP初识(一)

    TCP/IP学习记录,如有错误请指正,谢谢!!! 什么是TCP/IP协议? TCP/IP协议族分为四层(另一个名字是Internet协议族(Internet Protocol Suite)):链路层. ...

  9. Linux中将端口(80)重定向

    在Linux中直接指定命令: iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 其中80为要访问的端 ...

  10. canvas绘制圆角头像

    如果你想绘制的网页包含一个圆弧形的头像的canvas图片,但是头像本身是正方形的,需要的方法如下:首先, 拿到头像在画布上的坐标和宽高:(具体怎么获取不在此做具体介绍) 使用canvas绘制圆弧动画 ...