hihoCoder #1087 Hamiltonian Cycle

Description

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.

Input

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

Output an integer in a single line -- the number of different Hamiltonian Cycles in this graph.

Hint

Another sample:

Sample Input Sample Output

3 3

1 2

2 1

1 3

0

Sample Input

4 7
1 2
2 3
3 4
4 1
1 3
4 2
2 1

Sample Output

2

Solution:

考虑用 dynamic programming , 考虑搜索hamiltonian cycle 部分路径(固定搜索起点是节点1)如下 s(1)->x1->x2->.....->xn->e, 可知状态变量就是中间节点x1,x2,...,xn和进一步搜索起点e 用 bitset<13> mask(最多12个节点) 记录中间状态,mask.to_ulong() 最大值(2^14 -1), 所以用dp[e][mask.to_ulong()]表示状态。TALK IS CHEAP......

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <bitset>
 4 #include <cstring>
 5 using namespace std;
 6
 7 int aa;
 8 char buf[1<<15],*S=buf,*H=buf,ch;
 9 char getc(){return S==H&&(H=(S=buf)+fread(buf,1,1<<15,stdin),S==H)?0:*S++;}
10 int F(){
11     while(ch=getc(),ch<‘0‘||ch>‘9‘);aa=ch^‘0‘;
12     while(ch=getc()^‘0‘,ch>=0&&ch<=9)aa=(aa<<3)+(aa<<1)+ch;return aa;
13 }
14
15 int N;
16 int graph[201][201] = {0};
17 int path[13];
18 bitset<13> mask;
19
20
21 int dp[13][20000];
22
23 int hamCycle(int pos)
24 {
25     if (pos == N + 1) {
26         if(graph[path[pos-1]][path[1]])
27             return 1;
28         else
29             return 0;
30     }
31     int cnt = 0;
32     for (int n = 2; n <= N; ++n) {
33         if ((!mask[n]) && graph[path[pos-1]][n]) {
34             path[pos] = n;
35             mask.set(n);
36             if (dp[n][mask.to_ulong()] < 0)
37               dp[n][mask.to_ulong()] = hamCycle(pos+1);
38
39             cnt += dp[n][mask.to_ulong()];
40             mask.reset(n);
41         }
42     }
43
44     return cnt;
45 }
46
47
48 int main()
49 {
50     N = F();
51     int M = F();
52     int u,v;
53     for (int i = 0; i < M; ++i) {
54         u = F();
55         v = F();
56         graph[u][v] = 1;
57     }
58
59     for (int i = 1; i <= N; ++i) {
60         path[i] = -1;
61     }
62     path[1] = 1;
63     mask[1] = 1;
64     memset(dp, -1, sizeof dp);
65     cout << hamCycle(2) << endl;
66 }
时间: 2024-10-09 08:37:28

hihoCoder #1087 Hamiltonian Cycle的相关文章

PAT 1122 Hamiltonian Cycle

The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle". In this problem, you are supposed to tell if a given cycle is a Hamiltonian cycle. Input Specif

PAT 1122 Hamiltonian Cycle[比较一般]

1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle". In this problem, you are supposed to tell if a given cycle is a H

1122 Hamiltonian Cycle (25 分)

1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle". In this problem, you are supposed to tell if a given cycle is a H

PAT甲级——A1122 Hamiltonian Cycle【25】

The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle". In this problem, you are supposed to tell if a given cycle is a Hamiltonian cycle. Input Specif

计算机算法常用术语中英对照(分为两部分 其中一部分表格形式 )

第一部分 Data Structures 基本数据结构 Dictionaries 字典 Priority Queues 堆 Graph Data Structures 图 Set Data Structures 集合 Kd-Trees 线段树 Numerical Problems 数值问题 Solving Linear Equations 线性方程组 Bandwidth Reduction 带宽压缩 Matrix Multiplication 矩阵乘法 Determinants and Perm

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

算法设计与分析 - 李春葆 - 第二版 - pdf-&gt;word v3

1 1.1 第1章─概论 2 3 1.1.1 练习题 4 1. 下列关于算法的说法中正确的有( ). 5 Ⅰ.求解某一类问题的算法是唯一的 6 Ⅱ.算法必须在有限步操作之后停止 7 Ⅲ.算法的每一步操作必须是明确的,不能有歧义或含义模糊 8 Ⅳ.算法执行后一定产生确定的结果 9 A. 1个 B.2个 C.3个 D.4个 10 2. T(n)表示当输入规模为n时的算法效率,以下算法效率最优的是( ). 11 A.T(n)= T(n-1)+1,T(1)=1 B.T(n)= 2n2 12 C.T(n)

【转】编程词汇

很实用的编程英语词库,共收录一千五百余条词汇. 第一部分: application 应用程式 应用.应用程序 application framework 应用程式框架.应用框架 应用程序框架 architecture 架构.系统架构 体系结构 argument 引数(传给函式的值).叁见 parameter 叁数.实质叁数.实叁.自变量 array 阵列 数组 arrow operator arrow(箭头)运算子 箭头操作符 assembly 装配件 assembly language 组合语

北京地铁站点遍历最少经站次数问题普遍意义上是一个NP问题,目前不存在多项式时间算法能够解决该问题

http://www.cnblogs.com/jiel/p/5852591.html 众所周知求一个图的哈密顿回路是一个NPC问题: In the mathematical field of graph theory, a Hamiltonian path (or traceable path) is a path in an undirected or directed graph that visits each vertex exactly once. A Hamiltonian cycl