题目大意:给定一个 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. 模仿Spy++抓某窗口消息

    核心函数 SetWindowsHookExA API文档:https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-s ...

  2. BUUOJ reverse SimpleRev (爆破)

    SimpleRev SimpleRev(flag需加上flag{}再提交) 注意:得到的 flag 请包上 flag{} 提交 拖到ida 找到关键函数: unsigned __int64 Decry ...

  3. PTA(Basic Level)1023.组个最小数

    给定数字 0-9 各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就 ...

  4. [转帖]jdk8 Metaspace 调优

    jdk8 Metaspace 调优 https://blog.csdn.net/bolg_hero/article/details/78189621 转帖 简介 从JDK8开始,永久代(PermGen ...

  5. hive_UDTF函数

    hive的UDTF函数是可以输入一行数据然后输出多行多列(可以是单行/单列)的函数 public class Tex extends GenericUDTF { /** * 对传入的参数进行初始化 * ...

  6. 设计模式:解释器模式(Interpreter)

    为人处事是一门大学问,察言观色.听懂弦外之音都是非常重要的,老板跟你说“XX你最近表现平平啊,还得要多努力”,如果你不当回事,平常对待,可能下次就是“XX,恩,你人还是不错,平常工作也很努力,但是我想 ...

  7. CSP 最大的矩形(201312-3)

    问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...

  8. python-day43(正式学习)

    目录 复习 今日内容 字段操作 多表关系 外键 一对一:无级联关系 一对一:有级联关系 一对多 多对多 复习 """ 1.数据库的配置:my.ini [mysqld][m ...

  9. sql server 通配符

    sql有四种通配符: -- %可以匹配任意长度的字符: -- _匹配单个字符: -- [charlist]匹配括号中的任何一个字符 -- [^]匹配不在括号中的任意单个字符 示例: --'[ab]'匹 ...

  10. vm文件

    <html> <head> <title>编队管理</title> </head> <style type="text/cs ...