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 状态压缩]
题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...
随机推荐
- 模块的概念、模块的导入方式【IMPORT 模块名、FROM 模块 IMOPRT 功能】、模块的搜索路径、链式导入&循环导入
今日内容 1. 模块:模块的概念 2.导入的方式:import from import 3. 环境变量:sys.path 4. 导入模块的顺序 5. 循环导入:模块间互相导入 模块 常见的四种模块: ...
- cisco路由的ip静态路由添加(二)
路由器Corp 配置Corp(config)#ip route 192.168.20.0 255.255.255.0 10.1.3.2 150 Corp(config)#ip route 192.16 ...
- 关联SecureCRT
建议安装SecureCRT 8.1及其以上版本,打开http://EVE-NG地址/files/windows.zip下载eve-ng的Windows关联文件,然后使用文本编辑工具编辑win7_64b ...
- Autofac依赖注入容器
依赖注入容器-- Autofac https://github.com/danielpalme/IocPerformance Unity 更新频率高,微软的项目Grace 综合性能更高 目录: 一.简 ...
- Random Pick with Weight
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- windows vue环境搭建
windows环境搭建Vue开发环境 一.安装node.js(https://nodejs.org/en/) 下载完毕后,可以安装node,建议不要安装在系统盘(如C:). 二.设置nodejs pr ...
- hello1的web.xml解析
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" ...
- Python接口开发
一.flask flask是一个python编写的轻量级框架,可以使用它实现一个网站.web服务. 用flask开发接口的流程为: 1.定义一个server server=flask.Flask(__ ...
- 使用Python基于百度等OCR API的文字识别
百度OCR Baidu OCR API:一定额度免费,目前是每日500次 Python SDK文档:https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.htm ...
- Java工具类-基于SnowFlake的短地址生成器
Twitter的SnowFlake算法,使用SnowFlake算法生成一个整数,然后转化为62进制变成一个短地址URL /** * Twitter的SnowFlake算法,使用SnowFlake算法生 ...