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

这题是一道简单搜索题,我用dfs(idx,num1,num2)表示当前搜索的是idx的关系,num1表示虚拟关系的个数,num2表示现实关系的个数。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100060
#define ll long long
int num[10],gra[10][10],n,m,sum,guanxi[10][10],vis1[10],vis2[10]; void dfs(int idx,int num1,int num2,int pos,int from)
{
int i,j;
if(num1==num2 && num1+num2==num[idx]){
if(idx==n){
sum++;return;
}
else{
idx++;num1=num2=0;
for(i=1;i<=n;i++){
if(guanxi[idx][i]==1){
num2++;
}
else if(guanxi[idx][i]==0){
num1++;
}
}
dfs(idx,num1,num2,idx+1,0);
}
return ;
} if(num1>num[idx]/2 || num2>num[idx]/2)return;
for(i=pos;i<=n;i++){
if(gra[i][idx] && guanxi[i][idx]==-1){
guanxi[i][idx]=guanxi[idx][i]=0;
dfs(idx,num1+1,num2,i+1,1);
guanxi[i][idx]=guanxi[idx][i]=1;
dfs(idx,num1,num2+1,i+1,2);
guanxi[i][idx]=guanxi[idx][i]=-1;break;
}
}
return;
} int main()
{
int i,j,T,c,d,flag;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
if(n==1){
printf("1\n");continue;
}
memset(num,0,sizeof(num));
memset(gra,0,sizeof(gra));
for(i=1;i<=m;i++){
scanf("%d%d",&c,&d);
gra[c][d]=gra[d][c]=1;num[c]++;num[d]++;
}
flag=1;
for(i=1;i<=n;i++){
if(num[i]&1){
flag=0;break;
}
}
if(!flag){
printf("0\n");continue;
}
sum=0;
memset(guanxi,-1,sizeof(guanxi));
dfs(1,0,0,2,0);
printf("%d\n",sum);
}
return 0;
}

hdu5305 Friends的更多相关文章

  1. 解题报告 之 HDU5305 Friends

    解题报告 之 HDU5305 Friends Description There are  people and  pairs of friends. For every pair of friend ...

  2. 2015 多校联赛 ——HDU5305(搜索)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  3. hdu5305 Friends[状压dp]

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  4. hdu5305 Friends(dfs,多校题)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  5. 2015多校训练第二场 hdu5305

    把这题想复杂了,一直在考虑怎么快速的判断将选的边和已选的边无冲突,后来经人提醒发现这根本没必要,反正数据也不大开两个数组爆搜就OK了,搜索之前要先排除两种没必要搜的情况,这很容易想到,爆搜的时候注意几 ...

  6. hdu5305(2015多校2)--Friends(状压,深搜)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  7. hdu5305 Friends(dfs+map/hash)

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

随机推荐

  1. UVA - 185 Roman Numerals

    题目链接: https://vjudge.net/problem/UVA-185 思路: 剪枝.回溯 注意回溯的时候,是从当前点的下一个开始,而不是从已经遍历的个数点开始!!不然回溯有问题! 思路参考 ...

  2. 图解 ECDHE 密钥交换算法

    HTTPS 常用的密钥交换算法有两种,分别是 RSA 和 ECDHE 算法. 其中,RSA 是比较传统的密钥交换算法,它不具备前向安全的性质,因此现在很少服务器使用的.而 ECDHE 算法具有前向安全 ...

  3. Java开发工具类集合

    Java开发工具类集合 01.MD5加密工具类 import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...

  4. 转 Fiddler5 发送HTTP请求

    Fiddler5 发送HTTP请求  文章转自:https://www.cnblogs.com/zhengna/p/10879573.html 1.Fiddler Composer发送HTTP请求 C ...

  5. nodejs的调试debug

    目录 简介 开启nodejs的调试 调试的安全性 使用WebStorm进行nodejs调试 使用Chrome devTools进行调试 使用node-inspect来进行调试 其他的debug客户端 ...

  6. E2.在shell中正确退出当前表达式

    E2.在shell中正确退出当前表达式 优雅退出当前表达式 在shell里面输出复杂的多行表达时,经常由于少输入一个引号,一直无法退出当前的表达式求值,也没有办法终止它,以前只能通过两次Ctrl+C结 ...

  7. 页面切换提速30%!京东商城APP首屏耗时监控及优化实践

    https://mp.weixin.qq.com/s/vIG_x1MWC33HKV1GxalVHg 原创 平台研发朱跃棕等 京东零售技术 2020-12-09 网络接口请求可以从接口结构合理性及多接口 ...

  8. (转载)微软数据挖掘算法:Microsoft 线性回归分析算法(11)

    前言 此篇为微软系列挖掘算法的最后一篇了,完整该篇之后,微软在商业智能这块提供的一系列挖掘算法我们就算总结完成了,在此系列中涵盖了微软在商业智能(BI)模块系统所能提供的所有挖掘算法,当然此框架完全可 ...

  9. CSRF Laravel Cross Site Request Forgery protection¶

    Laravel 使得防止应用 遭到跨站请求伪造攻击变得简单. Laravel 自动为每一个被应用管理的有效用户会话生成一个 CSRF "令牌",该令牌用于验证授权用 户和发起请求者 ...

  10. css水平、垂直居中的写法

    水平居中 行内元素: text-align: center 块级元素: margin: 0 auto position:absolute +left:50%+ transform:translateX ...