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 ...
随机推荐
- 【bzoj题解】1012 最大数
题目描述 现在请求你维护一个数列,要求提供以下两种操作:1.查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2.插入操作.语法:A ...
- JDK1.8源码LinkedList
引用博文链接 : https://www.cnblogs.com/leskang/p/6029780.html LinkedList继承了 AbstractSequentialList抽象类,而不是像 ...
- 【CTF WEB】GCTF-2017读文件
读文件 只给了个1.txt可以读,试了一下加*不行,感觉不是命令执行,"../"返回上级目录也不行,猜测可能过滤了什么,在1.txt中间加上"./"发现仍能读取 ...
- linux bash shell之declare
一. #Set the right GC options based on the what we are runningdeclare -a server_cmds=("master&qu ...
- style一张图--openlayers
- 【前端vue开发】Hbuilder配置Avalon、AngularJS、Vue指令提示
偶尔也会研究一下前端内容,因为Hbuilder是基于eclipse开发的,所以用起来倍感亲切啊,而且在我尝试使用的几款前端开发工具中,Hbuilder的表现也是相当出色地,可以访问Huilder官网下 ...
- Java容器---迭代器
任何容器类,都必须有某种方式可以插入元素并将它们再次取回.毕竟,持有事物是容器最基本的工作. 对于List, add0是插入元素的方法之一,而get()是取出元素的方法之一. 如果从更高层的角度思考, ...
- Git missing in VS Code – No source control providers
解决办法:管理->设置->搜索[git.enabled]和[git.path],分别设置下即可. 注意"git.enabled: true",只设置git.path是不 ...
- LeetCode517. Super Washing Machines
You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...
- Jquery Datatable自动生成序号
公司项目里,使用的是jquerydatatable框架来展示数据的,在使用的时候遇到一个问题,就是要自动生成序号,且在分页的时候,下一页的序号紧跟着前一页的序号,下面是代码: "fnDraw ...