描述

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 (记忆化搜索)的更多相关文章

  1. [hihocoder 1033]交错和 数位dp/记忆化搜索

    #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1 ...

  2. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

  3. 【BZOJ-3895】取石子 记忆化搜索 + 博弈

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 263  Solved: 127[Submit][Status][Discuss] D ...

  4. hdu3555 Bomb (记忆化搜索 数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  5. zoj 3644(dp + 记忆化搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...

  6. loj 1044(dp+记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...

  7. DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects

    题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...

  8. HDU1978 记忆化搜索

    How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. bzoj4562: [Haoi2016]食物链--记忆化搜索

    这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...

随机推荐

  1. python之路----黏包的解决方案

    黏包的解决方案 远程执行命令 # server 下发命令 给client import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) ...

  2. PowerDesigner 教程

    摘自:http://www.cnblogs.com/advocate/p/3730027.html 目标:本文主要介绍PowerDesigner中概念数据模型 CDM的基本概念. 一.概念数据模型概述 ...

  3. csharp编写界面,opencv编写类库,解决 Pinvoke过程中的参数传递和平台调用问题

        使用csharp 编写winform程序,不仅速度快,而且容易界面美化并找到其他类库的支持:而使用 opencv编写图形图像处理程序,是目前比较流行,而且高效的一种方法.如果需要将两者结合,需 ...

  4. 在Android Studio中创建项目和模拟器

    北京电子科技学院 实      验      报      告 课程:移动平台应用开发实践  班级:201592  姓名:杨凤  学号:20159213 成绩:___________  指导老师:娄嘉 ...

  5. 20165310 java_blog_week4

    2165310 <Java程序设计>第4周学习总结 教材学习内容总结 继承(extends) 同一个包内:继承除了private修饰的变量与方法 不同包内:不继承private和友好,继承 ...

  6. Zigbee学习

    (一)Zigbee简介和开发环境快速建立(IAR) 1.我不是很清楚控制链条,对于Zigbee不是太清楚 答案:CC2530 芯片上集成了 8051 内核(增强型) 2.性能特点:低速率远距离,这造就 ...

  7. 在EditText里输入小写字母时,将小写字母转化为大写显示

    1.新建类继承ReplacementTransformationMethod 方法 public class test extends ReplacementTransformationMethod ...

  8. 联想笔记本thinkpad按F2不能直接重命名

    联想笔记本thinkpad按F2不能直接重命名,而是Fn+F2. 解决: 按一下Fn+Esc(一般在左上角)

  9. IntelliJ IDEA问题总结

    在使用Idea的过程中,会遇到各种各样的问题,下面我将在这里持续总结: 1.Unable to import maven project: See logs for details 在遇到这个问题时, ...

  10. go 通道

    1. package main import "fmt" func sum(s []int, c chan int) { sum := for _, v := range s { ...