时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

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状态压缩)的更多相关文章

  1. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  2. HDU 1074 Doing Homework (dp+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...

  3. hdu_4352_XHXJ's LIS(数位DP+状态压缩)

    题目连接:hdu_4352_XHXJ's LIS 题意:这题花大篇篇幅来介绍电子科大的一个传奇学姐,最后几句话才是题意,这题意思就是给你一个LL范围内的区间,问你在这个区间内最长递增子序列长度恰为K的 ...

  4. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. 【bzoj1076】[SCOI2008]奖励关 期望dp+状态压缩dp

    题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...

  6. hdu4336 Card Collector(概率DP,状态压缩)

    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...

  7. dp状态压缩

    dp状态压缩 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的就是那种状态很多,不容易用一般的方法表示的动态规划问题,这个就更加的难于把握了.难点在于以下几个方面:状 ...

  8. 洛谷 1052 dp 状态压缩

    洛谷1052 dp 状态压缩 传送门 (https://www.luogu.org/problem/show?pid=1052#sub) 做完这道题之后,感觉涨了好多见识,以前做的好多状压题目都是将一 ...

  9. NOIP2005过河[DP 状态压缩]

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

随机推荐

  1. 【HANA系列】SAP HANA SQL获取字符串长度

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL获取字符 ...

  2. JQuery II

    jQuery事件的绑定 <body> <button>点击1</button> <button>点击2</button> </body ...

  3. seq2seq&attention图解

  4. AS将一个项目导入到另一个项目中

    需求:有项目A,B.需要将B集成到A中,作为A的一个模块. 方法: 1.将B工程的app下面的build.gradle文字中  apply plugin: 'com.android.applicati ...

  5. c++ 读入优化通用模板

    struct ioss { #define endl '\n' ; char obuf[LEN], *oh = obuf; std::streambuf *fb; ioss() { ios::sync ...

  6. poj1050-最大子矩阵(dp)

    链接:http://poj.org/problem?id=1050 题意:给定n*n的矩阵,求和最大的子矩阵. 思路:我们将二维矩阵降维至一维,即将第i行到第j行的所有列压缩成一行,我们可以在线性时间 ...

  7. Mybatis(三) 动态SQL

    if + where 用法 1. if 元素来实现多条件查询 1.1 UserMapper.xml配置文件 <!--查询用户列表 (if)--> <select id="g ...

  8. linux 在线安装jdk

    1. yum -y list java* 2.yum -y install java-1.8.0-openjdk*

  9. 【pytorch】学习笔记(三)-激励函数

    [pytorch]学习笔记-激励函数 学习自:莫烦python 什么是激励函数 一句话概括 Activation: 就是让神经网络可以描述非线性问题的步骤, 是神经网络变得更强大 1.激活函数是用来加 ...

  10. FFmpeg4.0笔记:VS2019编译FFmpeg4.0源码

    0.下载TDM.msys和yasm 1.安装TDM-GCC-64 2.安装msys到TDM-GCC的安装目录中 3.将call "C:\Program Files (x86)\Microso ...