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条朋友关系,是朋友关系的两个人之间有两种联系 ...
随机推荐
- 转载 - Ubuntu源改国内源 与 批量更改ubuntu机器apt源
change_apt_source.sh # !/bin/bash # 备份原来的源文件 cp /etc/apt/sources.list /etc/apt/sources.list.bak # 获取 ...
- 【Spring】Spring的事务管理 - 2、声明式事务管理(实现基于XML、Annotation的方式。)
声明式事务管理 文章目录 声明式事务管理 基于XML方式的声明式事务 基于Annotation方式的声明式事务 简单记录 - 简单记录-Java EE企业级应用开发教程(Spring+Spring M ...
- Subline Text 3 安装
Subline Text 3 下载 下载链接 http://www.sublimetext.com/3 ,下载Subline Text3的安装包,这里以 64位的windows10为例,如果是其他操作 ...
- 【RAC】安装rac的时候。报错checking for oracle home incompatibilities failed
背景:由于oracle安装的时候中途出现了问题,解决过后,发现报错了 图形化安装的时候,有这个问题出现 解决办法: 删除安装过的所有缓存,和文件即可 1.删除ORACLE_BASE下的所有文件 2.删 ...
- Kafka 探险 - 生产者源码分析: 核心组件
这个 Kafka 的专题,我会从系统整体架构,设计到代码落地.和大家一起杠源码,学技巧,涨知识.希望大家持续关注一起见证成长! 我相信:技术的道路,十年如一日!十年磨一剑! 往期文章 Kafka 探险 ...
- MySQL调优性能监控之performance schema
一.performance_schema的介绍 performance:性能 schema:图(表)示,以大纲或模型的形式表示计划或理论. MySQL的performance schema 用于监控M ...
- 更新gitignore后如何使其生效
Files already tracked by Git are not affected; Git - gitignore Documentation https://git-scm.com/doc ...
- super 多重继承 super() function with multilevel inheritance
Python | super() function with multilevel inheritance - GeeksforGeeks https://www.geeksforgeeks.org/ ...
- LOJ1036
AHOI 2008 聚会 Y 岛风景美丽宜人,气候温和,物产丰富.Y 岛上有 N 个城市,有 N-1 条城市间的道路连接着它们.每一条道路都连接某两个城市.幸运的是,小可可通过这些道路可以走遍 Y 岛 ...
- SpringMVC听课笔记(六:视图和试图解析器)
1.spring mvc解析视图 2. 视图和视图解析器 3. 视图 4.常用的视图类 5.视图解析器 1) 2) 3) 4)JSTL 需要注意的是,配置了mvc:view-controller,为 ...