hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)
描述
Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us how many different Hamiltonian Cycles are there in this graph?
A Hamiltonian Cycle is a cycle that starts from some vertex, visits each vertex (except for the start vertex) exactly once, and finally ends at the start vertex.
Two Hamiltonian Cycles C1, C2 are different if and only if there exists some vertex i that, the next vertex of vertex i in C1 is different from the next vertex of vertex i in C2.
输入
The first line contains two integers n and m. 2 <= n <= 12, 1 <= m <= 200.
Then follows m line. Each line contains two different integers a and b, indicating there is an directed edge from vertex a to vertex b.
输出
Output an integer in a single line -- the number of different Hamiltonian Cycles in this graph.
提示
额外的样例:
样例输入 | 样例输出 |
3 3 1 2 2 1 1 3 |
0 |
样例输入
4 7
1 2
2 3
3 4
4 1
1 3
4 2
2 1
样例输出
2 题目大意:给一张无向图,找出哈密顿回路的条数。
题目分析:暴搜,不过搜起来要讲究技巧,用二进制数表示状态结合记忆化搜索才能不超时。 小技巧:sta&(-sta)便可得到只有二进制数sta最右边的1构成的数,如12&(-12)=8(负数在计算机内用反码表示)。 代码如下:
# include<iostream>
# include<cstdio>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
int n,m,dp[12][1<<12],st[12],p[1<<12];
int DP(int u,int s)
{
if(dp[u][s])
return dp[u][s];
if(!s)
return dp[u][s]=st[u]&1;
int rest=s&st[u];
while(rest){
int tp=rest&(-rest);
dp[u][s]+=DP(p[tp],s-tp);
rest-=tp;
}
return dp[u][s];
}
int main()
{
int a,b;
while(~scanf("%d%d",&n,&m))
{
memset(st,0,sizeof(st));
while(m--)
{
scanf("%d%d",&a,&b);
st[a-1]|=(1<<(b-1));
}
for(int i=0;i<n;++i)
p[1<<i]=i;
memset(dp,0,sizeof(dp));
printf("%d\n",DP(0,(1<<n)-2));
}
return 0;
}
hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)的更多相关文章
- [hihocoder 1033]交错和 数位dp/记忆化搜索
#1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1 ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 【BZOJ-3895】取石子 记忆化搜索 + 博弈
3895: 取石子 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 263 Solved: 127[Submit][Status][Discuss] D ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- zoj 3644(dp + 记忆化搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...
- loj 1044(dp+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...
- DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects
题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...
- HDU1978 记忆化搜索
How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- bzoj4562: [Haoi2016]食物链--记忆化搜索
这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...
随机推荐
- IDEA上传代码到码云
- 干货:Java并发编程系列之volatile(二)
接上一篇<Java并发编程系列之synchronized(一)>,这是第二篇,说的是关于并发编程的volatile元素. Java语言规范第三版中对volatile的定义如下:Java编程 ...
- SP211 PRIMIT - Primitivus recurencis(欧拉回路)
SP211 PRIMIT - Primitivus recurencis 欧拉回路 Warning: enormous Input/Output data 警告:巨大的输入/输出 经过若干(11)次提 ...
- ELK之elasticsearch6.5集群
前面介绍并初试了es6.5系列的单节点的操作,现在搭建es6.5系列的集群: 环境:三节点:master-172.16.23.128.node1-172.16.23.129.node2-172.16. ...
- 20145321《网络对抗技术》逆向与Bof基础
20145321<网络对抗技术>逆向与Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...
- 20165310java_blog_week6
2165310 <Java程序设计>第6周学习总结 教材学习内容总结 String 构造 String str=new String() String (char a[]) String ...
- 获取GetOpenFileName多选文件名
void CWriteWnd::OpenFileDialog() { OPENFILENAME ofn; TCHAR szOpenFileNames[*MAX_PATH] = _T("&qu ...
- Python3基础 os listdir curdir pardir 查看工作目录及其上一级目录的所有文件名
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解
题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈. 思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当 ...
- HDU3652 B-number(数位DP)题解
思路: 这里的状态分为3种,无13和末尾的1,无13且末尾为1,有13,然后DFS 等我搞清楚数位DP就来更新Orz 代码: #include<iostream> #include< ...