#1087 : Hamiltonian Cycle
- 样例输入
-
4 7 1 2 2 3 3 4 4 1 1 3 4 2 2 1
- 样例输出
-
2
描述
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.
提示
额外的样例:
样例输入 | 样例输出 |
3 3 1 2 2 1 1 3 |
0 |
代码如下:
/************************************************************************* > File Name: Hamiltionian_Cycle.cpp > Author: Zhanghaoran0 > Mail: chiluamnxi@gmail.com > Created Time: 2015年09月18日 星期五 21时21分17秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <bitset> #define OK 1 #define ERROR 0 using namespace std; int n ,m; int graph[210][210] = {0}; int path[13]; bitset<13> flag; int dp[13][100000]; int dfs(int pos){ if(pos == n + 1){ if(graph[path[pos - 1]][path[1]]) return OK; else return ERROR; } int ans = 0; for(int x = 2; x <= n; x ++){ if((!flag[x]) && graph[path[pos - 1]][x]){ path[pos] = x; flag.set(x); if(dp[x][flag.to_ulong()] < 0) dp[x][flag.to_ulong()] = dfs(pos + 1); ans += dp[x][flag.to_ulong()]; flag.reset(x); } } return ans; } int main(void){ int x, y; cin >> n >> m; for(int i = 0; i < m; i ++){ cin >> x >> y; graph[x][y] = 1; } for(int i = 1; i <= n; i ++){ path[i] = -1; } path[1] = 1; flag[1] = 1; memset(dp, -1, sizeof(dp)); cout << dfs(2) << endl; }