hdu 5305 Friends(2015多校第二场第6题)记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305
题意:给你n个人,m条关系,关系可以是online也可以是offline,让你求在保证所有人online关系的朋友和offline关系的朋友相等的情况下,这样的情况有多少种。
思路:因为online关系和offline关系的人数相等,而且m最多才28,所以只要枚举每个人的一半的关系是否符合要求即可,而且根据题意m是奇数或者有一个人的总关系为奇数那么就没有符合要求的情况,这样可以排除很多情况。
代码:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
#define LL __int64 int f[],on[],off[];
int m,n;
struct node
{
int x,y;
}nn[];
void init()
{
memset(f,,sizeof(f));
memset(on,,sizeof(on));
memset(off,,sizeof(off));
}
int dfs(int cot)
{
int x,y,i,ans;
ans=;
if(cot>=m)
{
for(i=;i<=n;i++)
{
if(on[i]!=off[i])
return ;
}
return ;
}
x=nn[cot].x;
y=nn[cot].y;
if(on[x]<f[x]/&&on[y]<f[y]/)
{
on[x]++;
on[y]++;
ans+=dfs(cot+);
on[x]--;
on[y]--;
}
if(off[x]<f[x]/&&off[y]<f[y]/)
{
off[x]++;
off[y]++;
ans+=dfs(cot+);
off[x]--;
off[y]--;
}
return ans;
} int main()
{
int i,T,ans;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d%d",&n,&m);
init();
for(i=;i<m;i++)
{
scanf("%d%d",&nn[i].x,&nn[i].y);
f[nn[i].x]++;
f[nn[i].y]++;
}
if(m&)
{
printf("0\n");
continue;
}
int flag=;
for(i=;i<=n;i++)
{
if(f[i]&)
{
flag=;
break;
}
}
if(flag)
{
printf("0\n");
continue;
}
ans=dfs();
printf("%d\n",ans);
}
}
return ;
}
hdu 5305 Friends(2015多校第二场第6题)记忆化搜索的更多相关文章
- hdu 5308 (2015多校第二场第9题)脑洞模拟题,无语
题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=44 题意:给你n个n,如果能在n-1次运算之后(加减乘除)结果为24的输出n-1次运算的过程,如果不能输 ...
- hdu 5301 Buildings (2015多校第二场第2题) 简单模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题意:给你一个n*m的矩形,可以分成n*m个1*1的小矩形,再给你一个坐标(x,y),表示黑格子 ...
- 【HDU 5305】Friends 多校第二场(双向DFS)
依据题意的话最多32条边,直接暴力的话 2 ^ 32肯定超时了.我们能够分两次搜索时间复杂度降低为 2 * 2 ^ 16 唯一须要注意的就是对眼下状态的哈希处理. 我採用的是 十进制表示法 跑的还是 ...
- 多校5 1001 HDU5781 ATM Mechine 记忆化搜索+概率
// 多校5 1001 HDU5781 ATM Mechine // http://acm.hdu.edu.cn/search.php?field=problem&key=2016+Multi ...
- HDU 5301 Buildings(2015多校第二场)
Buildings Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
- HDU 5308 I Wanna Become A 24-Point Master(2015多校第二场)
I Wanna Become A 24-Point Master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 ...
- HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- hdu 5294 Tricks Device(2015多校第一场第7题)最大流+最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意:给你n个墓室,m条路径,一个人在1号墓室(起点),另一个人在n号墓室(终点),起点的那 ...
- HDU 5289 Assignment(2015 多校第一场二分 + RMQ)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
随机推荐
- equals方法变量和常量位置区别
对于字符串比较,我的习惯用法是 变量.equals(常量) 比如: a.equals("a") 今天看视频才知道变量在前面与后面有很大影响,正确的写法是常量放前面(可以 ...
- 怎么修改oracle用户密码
在以SYSDBA身份登陆时可以修改其他用户的密码,比如: SQL> alter user user01 identified by user10;
- 如何编译和安装libevent【转】
转自:http://www.open-open.com/lib/view/open1455522194089.html 来自: http://blog.csdn.net/yangzhenping/ar ...
- container_of分析【转】
转自:http://blog.csdn.net/tigerjibo/article/details/8299589 1.container_of宏 1> Container_of在Linux内核 ...
- Serv-U 的升级及数据备份和迁移【转】
Serv-U 配置备份 在serv-u7.x及以上版本安装目录下,有一个文件Serv-U.Archive是serv-u的配置文件,有一个users文件夹是Serv-U的域和用户的信息,那么我们只需 ...
- pom可以过滤resource 下的文件
- 2.Centos-Docker-shipyard中文版安装
1.准备(所有服务器都要执行) a.设置主机名和hosts vi /etc/hosts 192.168.1.2 centos-master 192.168.1.4 centos-minion-2 b. ...
- 17 Go Slices: usage and internals GO语言切片: 使用和内部
Go Slices: usage and internals GO语言切片: 使用和内部 5 January 2011 Introduction Go's slice type provides a ...
- dc
http://www.cnblogs.com/yjmyzz/p/docker-install-tutorial.html https://segmentfault.com/a/119000000510 ...
- No.5 selenium学习之路之多窗口句柄
多窗口相关操作 获取当前句柄 c_handle = driver.current_window_handle 获取所有句柄 all_handle = driver.window_handles 切换到 ...