hdu5305 Friends
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.
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.
3 3
1 2
2 3
3 1
4 4
1 2
2 3
3 4
4 1
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的更多相关文章
- 解题报告 之 HDU5305 Friends
解题报告 之 HDU5305 Friends Description There are people and pairs of friends. For every pair of friend ...
- 2015 多校联赛 ——HDU5305(搜索)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- hdu5305 Friends[状压dp]
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- hdu5305 Friends(dfs,多校题)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 2015多校训练第二场 hdu5305
把这题想复杂了,一直在考虑怎么快速的判断将选的边和已选的边无冲突,后来经人提醒发现这根本没必要,反正数据也不大开两个数组爆搜就OK了,搜索之前要先排除两种没必要搜的情况,这很容易想到,爆搜的时候注意几 ...
- hdu5305(2015多校2)--Friends(状压,深搜)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- hdu5305 Friends(dfs+map/hash)
题目:pid=5305">http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给定N个人和M条朋友关系,是朋友关系的两个人之间有两种联系 ...
随机推荐
- Linux性能相关命令
Linux性能相关命令 目录 Linux性能相关命令 1. 查看硬盘相关信息 2. 查看CPU相关信息 3. 查看内存相关信息 4. 查看进程运行的信息 1. 查看硬盘相关信息 cat /proc/s ...
- 【RAC】双节点RAC搭建
本文主要是双节点的RAC进行搭建,根据黄伟老师的视频进行总结和使用. 搭建环境: 1.两台安装好Linux_x64系统的服务器 2.IP设置 注意:Priv-IP的IP是自己一个网段,而剩下的SCAN ...
- 关于BAPI_TRANSACTION_COMMIT一点说明
我们调用bapi做了相关的业务操作后,通常都要在后面调用 BAPI_TRANSACTION_COMMIT来提交所做得更改 然而,有时候,在程序中需要调用多个不同的BAPI实现不同的功能,那么这个时候就 ...
- JMM在X86下的原理与实现
JMM在X86下的原理与实现 Java的happen-before模型 众所周知 Java有一个happen-before模型,可以帮助程序员隔离各个平台多线程并发的复杂性,只要Java程序员遵守ha ...
- 2.4V升3.3V,2.4V升3V,1A大电流升压芯片
两节镍氢电池串联就是1.2V+1.2V=2.4V的供电电压了,2.4V升3V, 2.4V升3.3V的话,就能稳压稳定给模块供电了,镍氢电池是会随着使用的电池电量减少的话,电池的电压也是跟着变化的,导致 ...
- Android 代码规范大全
前言 虽然我们项目的代码时间并不长,也没经过太多人手,但代码的规范性依然堪忧,目前存在较多的比较自由的「代码规范」,这非常不利于项目的维护,代码可读性也不够高, 此外,客户端和后端的研发模式也完全不同 ...
- 在HTML中改变input标签中的内容
在HTML中改变input标签的内容 1.使用js自带的方法: document.getElementById('roadName').value='武汉路';//通过标签选择器来选择标签,然后设置值 ...
- 一个非常棒的Go-Json解析库
json是一种数据格式,经常被用作数据交换,页面展示,序列化等场景,基本每种语言都有对应的json解析框架,Go语言也不例外,并且内置了json库,基本能够满足一些普通开发场景,但有些复杂场景下就不太 ...
- uni-app开发经验分享二: uni-app生命周期记录
应用生命周期(仅可在App.vue中监听) 页面生命周期(在页面中添加) 当页面中需要用到下拉刷新功能时,打开pages.json,在"globalStyle"里设置"e ...
- Py-多态,封装,反射,描述符,包装标准类型,面向对象进阶
多态: 对象可以通过他们共同的属性和动作来访问,而不需要考虑他们的类多态是继承的应用 class H2o: def __init__(self,temp): self.temp=temp def ht ...