hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)

时间:2022-09-07 18:34:37

描述

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. &lbrack;hihocoder 1033&rsqb;交错和 数位dp&sol;记忆化搜索

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

  2. &lbrack;ACM&lowbar;动态规划&rsqb; 数字三角形&lpar;数塔&rpar;&lowbar;递推&lowbar;记忆化搜索

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

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

    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&lpar;dp &plus; 记忆化搜索&rpar;

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

  6. loj 1044&lpar;dp&plus;记忆化搜索)

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

  7. DP&lpar;记忆化搜索&rpar; &plus; 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&colon; &lbrack;Haoi2016&rsqb;食物链--记忆化搜索

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

随机推荐

  1. MySQL 查看表结构简单命令。

    一.简单描述表结构,字段类型 desc tabl_name; 显示表结构,字段类型,主键,是否为空等属性,但不显示外键. 二.查询表中列的注释信息 select * from information_ ...

  2. 相关Python分割操作

    刚论坛python文本 http://bbs.byr.cn/#!article/Python/1693 攻克了一个关于python分片的问题. 问题: uesrList = ['1','2','3', ...

  3. &period;NET 知识整理笔记

    本文纯属自己理解着写的,如果有什么错误或者不全面希望读者能够加以补充!~ 1.Web.Config配置文件的节点操作. 在Web.Config中写入. <configuration> &l ...

  4. iOS中 UIProgressView 技术分享

    UIProgressView 继承自UIView,用来显示进度的,如音乐,视频的缓冲进度,文件的上传下载进度等.让用户知道当前操作完成了多少,离操作结束还有多远 AppDelegate.m Progr ...

  5. Python发送邮件脚本

    import smtplib from email.mime.text import MIMEText mailserver = "smtp.163.com" username_s ...

  6. Pac-Man 吃豆人

    发售年份 1980 平台 街机 开发商 南梦宫(Namco) 类型 迷宫 https://www.youtube.com/watch?v=dScq4P5gn4A

  7. Chapter 6 链表(上):如何实现LRU缓存淘汰算法?

    缓存淘汰策略: 一.什么是链表? 1.和数组一样,链表也是一种线性表. 2.从内存结构来看,链表的内存结构是不连续的内存空间,是将一组零散的内存块串联起来,从而进行数据存储的数据结构. 3.链表中的每 ...

  8. 【C&sol;C&plus;&plus;】查找(一):静态查找表

    {静态查找表 + 动态查找表} 所谓动态,就是,找的时候没有则添加,或者能删除 关键字:primary key:用来表示查找表中的一条记录 {主关键字 + 次关键字} 主关键字是唯一的,用来唯一的标识 ...

  9. js 2017 - 2

    设置360为极速模式   <meta name='renderer' content='webkit'> css3超出隐藏 .ellipsis { // 超出一行 width: 100%; ...

  10. 关于chrome控制台警告:Synchronous XMLHttpRequest on the main thread

    Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to th ...