标签:
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.
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 an integer in a single line -- the number of different Hamiltonian Cycles in this graph.
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 }
hihoCoder #1087 Hamiltonian Cycle
标签:
原文地址:http://www.cnblogs.com/liew/p/4396482.html