HihoCoder1087Hamiltonian Cycle(DP状态压缩)
描述
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
搜索大概也可以搞定。
- 求哈密顿环的数目
- 既然是环,且每个点都经过,我们假定一个起点,得到的结果是一样的,我的代码假定的是1为起点。
- 这题有重边,但是必须两个点之间重边只看成一条边才能AC
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;
int Mp[][];
int dp[<<][];//vis,now
int main()
{
int n,m,x,y,i,k,p,ans=;
scanf("%d%d",&n,&m);
for(i=;i<=m;i++){
scanf("%d%d",&x,&y);
Mp[x][y]=;
}
for(i=;i<=n;i++) dp[][]=;
for(i=;i<(<<n);i++)
{
for(k=;k<=n;k++)//now
{
if(!(i&(<<(k-)))) continue;
for(p=;p<=n;p++)//pre
{
if(!(i&(<<(p-)))||k==p) continue;
dp[i][k]=dp[i][k]+dp[i^(<<(k-))][p]*Mp[p][k];
}
}
}
for(i=;i<=n;i++) ans+=dp[(<<n)-][i]*Mp[i][];
printf("%d\n",ans);
return ;
}
HihoCoder1087Hamiltonian Cycle(DP状态压缩)的更多相关文章
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...
- hdu_4352_XHXJ's LIS(数位DP+状态压缩)
题目连接:hdu_4352_XHXJ's LIS 题意:这题花大篇篇幅来介绍电子科大的一个传奇学姐,最后几句话才是题意,这题意思就是给你一个LL范围内的区间,问你在这个区间内最长递增子序列长度恰为K的 ...
- hdu 4352 数位dp + 状态压缩
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【bzoj1076】[SCOI2008]奖励关 期望dp+状态压缩dp
题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...
- hdu4336 Card Collector(概率DP,状态压缩)
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- dp状态压缩
dp状态压缩 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的就是那种状态很多,不容易用一般的方法表示的动态规划问题,这个就更加的难于把握了.难点在于以下几个方面:状 ...
- 洛谷 1052 dp 状态压缩
洛谷1052 dp 状态压缩 传送门 (https://www.luogu.org/problem/show?pid=1052#sub) 做完这道题之后,感觉涨了好多见识,以前做的好多状压题目都是将一 ...
- NOIP2005过河[DP 状态压缩]
题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...
随机推荐
- SparkCore的性能优化
1.广播变量 1.1. Spark提供的Broadcast Variable,是只读的,并且在每个节点上只会有一份副本,而不会为每个task都拷贝一份副本 1.2.它的最大作用,就是减少变量到各个节点 ...
- app测试基础知识之命令
app测试点:功能测试,安全测试,用户体验测试,交叉事件测试,兼容性测试,性能测试,安装/升级/卸载 ,UI测试 命令操作: adb connect 名 adb devices adb instal ...
- selenium知识思维导图|从元素定位到操作断言,助你快速入门自动化测试
为什么要进行自动化测试? 缩短测试周期,节省成本. 避免人为出错,提高准确性和可靠性. 获取需求覆盖率,代码覆盖率,提供衡量软件质量的指标. 自动化测试的条件? 手工测试完成后. 项目周期长,需求稳定 ...
- VMware重装:网络适配器驱动安装失败解决办法
参考链接:https://blog.csdn.net/theConqueror/article/details/80449125
- (已解决)Could not open '/var/lib/nova/mnt/*/volume-*': Permission denied
[问题描述] 创建boot_from_volume的虚机时,磁盘后端为NFS,创建失败. [错误日志] nova-compute模块 Could not open '/var/lib/nova/mnt ...
- 第五周课程总结&试验报告三
第五周课程总结 一.第五周课程总结 1.this关键字 this可用于任何实例方法内指向当前对象,也可指向对其调用当前方法的对象,或者在需要当前类型对象引用时使用.当一个类的属性(成员变量)名与访问该 ...
- linux 下各errno的意义(转)
linux 下各errno的意义(转) 本文转自:http://blog.csdn.net/kofiory/article/details/5790409 strerror(errno):获取er ...
- Kinect开发-Hello Kinect
置好开发环境后,首先测试下是否真的完成,也就是能够正常进行开发.此时,当然就得祭出Hello World大法! 1.首先创建一个WPF Application工程,之后添加对Microsoft.Kin ...
- Iterator<E>接口
https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html public interface Iterator<E> ...
- 程序员必备的网站之Tutorialspoint
程序员必备的网站之Tutorialspoint 给大家介绍一个非常好的网站Tutorialspointhttp://www.tutorialspoint.com/index.htm,也许好多人都已经用 ...