题目大意:给定一个 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. 组件推荐Forloop.HtmlHelpers 用来实现MVC的js加载顺序

    最近在开发的时候遇到js加载顺序的问题,layui在底部声明了js,但是我想在页面其他地方使用分布视图,分布视图内有自己的js逻辑,发现不能执行,一看就发现,这里的js应该加在layui后面执行才能有 ...

  2. linux新建文件夹

    mkdir -p .... -p  ----parents no error if existion, make parent directories as needed

  3. 网络实验 04-利用三层交换机实现VLAN间路由

    利用三层交换机实现VLAN间路由 一.实验目标 掌握交换机Tag VLAN 的配置 掌握三层交换机基本配置方法 掌握三层交换机VLAN路由的配置方法 通过三层交换机实现VLAN间相互通信 二.实验背景 ...

  4. XCTF (app1)

    打开app.一个文本框,随便输入提示如下图. 打开JEB反编译. v2调用getPackageInfo获取版本信息.一般 Android 通过 PackageInfo 这个类来获取应用安装包信息,比如 ...

  5. random、json、pickle、hashlib、hmac、shutil、shevle模块

    今日内容: 1. random 模块 2. json模块 3. pickle 模块 4.hashlib 模块 5. hmac 模块 6. shutil 模块 7. shelve 模块 1. rando ...

  6. PhpMyAdmin提示未加密

    新版本的PhpMyAdmin 增强了安全性,需要在配置文件设置一个短语密码.否则进入之后会有“配置文件现在需要一个短语密码.”的红色警叹提示. 解决方法: .将 phpMyAdmin/librarie ...

  7. 好用的 Chrome 插件

    这些好用的 Chrome 插件,提升你的工作效率   本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可 ...

  8. js继承的方式及其优缺点

    js继承方法 前因:ECMAScript不支持接口继承,只支持实现继承 一.原型链 概念:每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针,让 ...

  9. git与码云关联教程

    注:我用的这种方法是利用“公钥”使本地仓库与码云建立起联系,从而不需要用户名与密码的方法. 1.首先,创建好码云,再在码云上创建好个人仓库,然后复制好仓库地址,这里的地址是指“SSH”类型的. 2.下 ...

  10. 如何获取字符串中某个具体的数值--通过json.load转化成字典形式获取

    r=requests.get('http://httpbin.org/get').text print(r) # print(type(r)) # 如果要获取User-Agent的详细数值,需要做JS ...