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. Java类的加载过程-重点!!

    java类的加载过程有以下几步共同完成: 加载->连接->初始化.连接又分为验证.准备.解析 一个非数组类的加载阶段(加载阶段获取类的二进制字节流的动作)是可控性最强的阶段,这一步我们可以 ...

  2. .NET 云原生架构师训练营(模块二 基础巩固 敏捷开发)--学习笔记

    2.7.1 敏捷开发 敏捷介绍 敏捷的起源 敏捷软件开发宣言 敏捷开发十二原则 生命周期对比 敏捷开发的特点 敏捷的发展 敏捷的核心 敏捷的起源 2001年,17个老头子在一起一边滑雪,一边讨论工作, ...

  3. Java 基于 mysql-connector-java 编写一个 JDBC 工具类

    用到的 jar 包 jar包地址: mysql-connector-java-5.1.47.jar junit-4.13.jar Maven: <!-- mysql驱动 --> <d ...

  4. Spring Cloud微服务Sentinel+Apollo限流、熔断实战总结

    在Spring Cloud微服务体系中,由于限流熔断组件Hystrix开源版本不在维护,因此国内不少有类似需求的公司已经将眼光转向阿里开源的Sentinel框架.而以下要介绍的正是作者最近两个月的真实 ...

  5. python optparse模块的用法

    引用原博主文章链接: https://www.cnblogs.com/darkpig/p/5717902.html

  6. uni-app 开发随笔(踩坑记录)

    这里总结一些uni-app开发时我遇到的坑 uni-app获取元素高度及屏幕高度(uni-app不可使用document) uni.getSystemInfo({ success: function( ...

  7. JSAAS BPM快速开发平台-企业管理软件,专属你的企业管家

    前言: 2020年,企业该如何去选择合适的信息化规划管理软件,基于目前社会软件杂乱无章,选择企业业务贴近的管理软件,甚是困难,市场上一些大品牌公司的产品,定位高,价格高,扩展难,等等一系列的问题,对于 ...

  8. linux设备

    设备初始化时同样要执行一个device_register函数,该函数传入一个struct device *类型的指针,因此要定义一个struct device类型的变量作为我们的设备. struct ...

  9. 网络优化之net.ipv4.tcp_tw_recycle和tcp_tw_reuse参数

    网络优化之net.ipv4.tcp_tw_recycle和tcp_tw_reuse参数 - 一个人默默潜行 - 博客园 https://www.cnblogs.com/ppp1314520818/p/ ...

  10. https://tools.ietf.org/html/rfc8017

    PKCS #1: RSA Cryptography Specifications Version 2.2