题目大意:给定一个 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的更多相关文章

  1. 【题解】Shortest Cycle

    原题链接:CF1205B   题目大意   给定\(n\)个整数\(a_1,a_2,a_3, \dots ,a_n\),若\(i \neq j\)且\(a_i \land a_j \neq 0\),则 ...

  2. PAT甲级——A1122 Hamiltonian Cycle【25】

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  3. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  4. 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 ...

  5. 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 ...

  6. 【题解】cycle

    [题解]cycle 题目描述 给定一个无向图,求一个环,使得环内边权\(\div\)环内点数最大. 数据范围 \(n \le 5000\) \(m\le 10000\) \(Solution\) 考虑 ...

  7. 【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的. 即最后会形成若干个环. g[i]显然等于那个环的大 ...

  9. 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. Centos7永久关闭防火墙

    Centos7永久关闭防火墙 查看防火墙状态: systemctl status firewalld.service 绿的running表示防火墙开启 执行关闭命令: systemctl stop f ...

  2. Linux编辑网络连接

    Linux编辑网络连接   实验目标: 通过本实验掌握新建网络连接.修改hosts文件.修改主机名的方法. 实验步骤: 1.新建一个名为review的网络连接,并配置ip地址,启用新连接 2.修改ho ...

  3. 解决anaconda安装cvxpy失败的方法

    在Windows下安装凸优化包CVXPY 直接在anaconda prompt中输入pip install cvxpy经常会出现安装失败的情况,使用以下方法,亲测成功! 1. 下载所需的whl文件,请 ...

  4. 1137. N-th Tribonacci Number(Memory Usage: 13.9 MB, less than 100.00% of Python3)

    其实思路很简单,套用一下普通斐波那契数列的非递归做法即可,不过这个成绩我一定要纪念一下,哈哈哈哈哈 代码在这儿: class Solution: def tribonacci(self, n: int ...

  5. Js 原型,原型链

    原型,原型链 原型:在JavaScript中原型是一个prototype对象,用于表示类型之间的关系. 原型链:JavaScript万物都是对象,对象和对象之间也有关系,并不是孤立存在的.对象之间的继 ...

  6. java循环队列实现代码

    public class Queue { //队首指针 private int front; //队尾指针 private int rear; //数组 private int[] arr; //数组 ...

  7. 初试spark java WordCount

    初始环境:OS X 10.10.5 准备:boot2docker 进入boot2docker后安装 docker-spark  地址: https://github.com/sequenceiq/do ...

  8. nginx+php设置大文件请求上传(502及504问题处理)

    502问题 php-fpm 修改项: request_terminate_timeout 位置: eg: /etc/php5/fpm2/pool.d/www.conf ; The timeout fo ...

  9. python 运算符与流程控制

    运算符与流程控制 运算符 赋值运算 用'='表示,'='的左边只能是变量 算术运算 +.-.*:加.减.乘 /:除法运算,运算结果为浮点数 //:除法运算,运算结果为整数(商) %:求余 **:求幂 ...

  10. vue实现登录路由拦截

    第一步 在router.js里面 把需要判断是否登录的路由添加meta对象属性 在meta对象里面自定义一个属性值 第二步 : 在router.js里面 与const router = new Rou ...