【hiho1087】Hamiltonian Cycle
题目大意:给定一个 N 个点的有向图,计数图上哈密顿回路的条数。
题解:哈密顿回路需要经过除了初始位置,每个点恰好一次。如果已知一条哈密顿回路的方向,那么从这条路上任意一个点出发,得到的都是同样的结果。因此,不妨设从 0 号节点出发,最后回到 0 号节点。统计答案只需要枚举最后一个点在哪个位置即可。
代码如下
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,m,G[12][12];
LL f[1<<12][12],ans;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
G[--x][--y]=1;
}
f[1][0]=1;
for(int s=0;s<1<<n;s++){
for(int i=0;i<n;i++){
if((s>>i&1)==0)continue;
for(int j=0;j<n;j++){
if((s>>j&1)||!G[i][j])continue;
f[s|(1<<j)][j]+=f[s][i];
}
}
}
for(int i=0;i<n;i++){
if(G[i][0]==0)continue;
ans+=f[(1<<n)-1][i];
}
printf("%lld\n",ans);
return 0;
}
【hiho1087】Hamiltonian Cycle的更多相关文章
- 【题解】Shortest Cycle
原题链接:CF1205B 题目大意 给定\(n\)个整数\(a_1,a_2,a_3, \dots ,a_n\),若\(i \neq j\)且\(a_i \land a_j \neq 0\),则 ...
- PAT甲级——A1122 Hamiltonian Cycle【25】
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 【题解】cycle
[题解]cycle 题目描述 给定一个无向图,求一个环,使得环内边权\(\div\)环内点数最大. 数据范围 \(n \le 5000\) \(m\le 10000\) \(Solution\) 考虑 ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的. 即最后会形成若干个环. g[i]显然等于那个环的大 ...
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 04 npm 命令大全
一.npm简介 npm(Node Package Manager)是随同node.js 一起安装的包管理工具,为了解决nodejs代码部署上的很多问题,常用以下场景: 允许用户从npm服务器下载别 ...
- linux下杀进程的方法
http://www.linuxidc.com/Linux/2011-08/40052.htm kill -s 9 2222
- Golang- import 导入包的几种方式:点,别名与下划线
包的导入语法 在写Go代码的时候经常用到import这个命令用来导入包文件,看到的方式参考如下: import( "fmt" ) 然后在代码里面可以通过如下的方式调用 fmt.Pr ...
- MaxScale中间件部署数据库读写分离
操作系统:CentOS7 (Core) 数据库:MariaDB-10.2.6-linux-glibc_214-x86_64 MaxScale服务器:192.168.40.134 主服务器:192.16 ...
- Git-T
或在命令行上创建一个新的存储库echo“#gittest”>> README.md git init git add README.md git commit -m“first commi ...
- DOM事件练习 II
select框联动效果 需求:当在textarea中输入内容,点击留言按钮,会添加到浏览器中,最新留言出现在最顶端. <!DOCTYPE html> <html lang=" ...
- 【神经网络与深度学习】什么是HDF
什么是HDF HDF 是用于存储和分发科学数据的一种自我描述.多对象文件格式.HDF 是由美国国家超级计算应用中心(NCSA)创建的,以满足不同群体的科学家在不同工程项目领域之需要.HDF 可以表示出 ...
- c++ 读入优化通用模板
struct ioss { #define endl '\n' ; char obuf[LEN], *oh = obuf; std::streambuf *fb; ioss() { ios::sync ...
- Kick Start 2019 Round F Teach Me
题目链接 题目大意 有 $N$ 个人,$S$ 项技能,这些技能用 $1, 2, 3, \dots, S$ 表示 .第 $i$ 个人会 $c_i$ 项技能($ 1 \le c_i \le 5 $).对于 ...
- 走环概率问题(至今有点迷)--牛客第二场( Eddy Walker)
思路: 概率结论题,好像属于线性递推,现在也不太懂(lll¬ω¬) #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include < ...