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. git 遇到 fatal: loose object xxxx (stored in .git/objects/cb/xxxx) is corrupt 问题

    我的git版本是2.3.x,用下面这个参考链接的方法也可以解决 参考blog

  2. kubernets之ReplicaSet

    一   介绍RS 1.1   RS与RC在功能上基本上是一摸一样的,因为两者的功能都是用来管控集群内部的pod,并且 两者都具备模版,副本数量以及标签选择器等三要素,区别点在于,RS拥有着更为强大的标 ...

  3. xray—学习笔记

    长亭xray扫描器 简介 xray (https://github.com/chaitin/xray) 是从长亭洞鉴核心引擎中提取出的社区版漏洞扫描神器,支持主动.被动多种扫描方式,自备盲打平台.可以 ...

  4. Spring Boot 2.x基础教程:配置元数据的应用

    在使用Spring Boot开发应用的时候,你是否有发现这样的情况:自定义属性是有高量背景的,鼠标放上去,有一个Cannot resolve configuration property的配置警告. ...

  5. C# url的编码解码,xml和json的序列化和反序列化

    参考中国慕课网dot net web编程应用程序实践 using System; using System.Collections.Generic; using System.IO; using Sy ...

  6. python中hmac模块的使用

    hmac(hex-based message authentication code)算法在计算哈希的过程中混入了key(实际上就是加盐),和hashlib模块中的普通加密算法相比,它能够防止密码被撞 ...

  7. 大数据系列2:Hdfs的读写操作

    在前文大数据系列1:一文初识Hdfs中,我们对Hdfs有了简单的认识. 在本文中,我们将会简单的介绍一下Hdfs文件的读写流程,为后续追踪读写流程的源码做准备. Hdfs 架构 首先来个Hdfs的架构 ...

  8. Centos 7.x系统下忘记用户登录密码,重置root密码的方法

    转载的,作为一个参考保存.谢谢:https://blog.csdn.net/userpass_word/article/details/81807316 1.开机后进入以下界面,然后按Esc或者E键编 ...

  9. 浅析Linux用户空间中的Mmap

    一.MMap基础概念 mmap是一种内存映射文件的方法,即将一个文件或者其它对象映射到进程的地址空间,实现文件磁盘地址和进程虚拟地址空间中一段虚拟地址的一一对映关系.实现这样的映射关系后,进程就可以采 ...

  10. 我教你如何解决 Docker 下载 mcr.microsoft.com 镜像慢的办法

    我教你如何解决 Docker 下载 mcr.microsoft.com 镜像慢的办法 一.介绍 最近,我在写有关使用 Jenkins 搭建企业级持续集成环境的文章,准备了四台服务器,企业级别嘛,一台就 ...