P8281 "MCOI-08" Fast Enumeration

Description

Technoblade abstracts Skyblock as a simple directed graph with $n$ nodes ($n\le 50$) and $m$ edges. He needs to find **all** Hamiltonian cycles in this graph, i.e., all permutations $p_1,p_2,\dots,p_n$ such that $p_1=1$ and $p_1\to p_2\to \dots\to p_{n-1}\to p_n\to p_1$ is a valid path. **The testdata guarantees that the number of Hamiltonian cycles is non-zero and less than $10^4$.** **The testdata is sampled randomly from all valid inputs.**

Input Format

The first line contains two positive integers $n,m$. The next $m$ lines each contain two positive integers $u,v$.

Output Format

Output several lines. Each line outputs $n$ positive integers, representing one Hamiltonian cycle. Output them in increasing lexicographical order.

Explanation/Hint

#### Explanation for Sample 1 ![](https://cdn.luogu.com.cn/upload/image_hosting/lth4lrb1.png) There is $1$ Hamiltonian cycle: - $1\to2\to3\to1$. #### Explanation for Sample 2 ![](https://cdn.luogu.com.cn/upload/image_hosting/if0vz8gm.png) There is $1$ Hamiltonian cycle: - $1\to3\to4\to2\to1$. #### Explanation for Sample 3 ![](https://cdn.luogu.com.cn/upload/image_hosting/dv1tul62.png) There are $2$ Hamiltonian cycles: - $1\to2\to3\to4\to5\to1$; - $1\to2\to5\to3\to4\to1$. #### Explanation for Sample 4 ![](https://cdn.luogu.com.cn/upload/image_hosting/wggv2mfd.png) There are $2$ Hamiltonian cycles: - $1\to2\to3\to4\to5\to1$; - $1\to3\to5\to2\to4\to1$. #### Explanation for Sample 5 ![](https://cdn.luogu.com.cn/upload/image_hosting/cfi80wob.png) There are $3$ Hamiltonian cycles: - $1\to5\to2\to3\to4\to6\to1$; - $1\to5\to2\to4\to6\to3\to1$; - $1\to5\to3\to4\to6\to2\to1$. #### Explanation for Sample 6 ![](https://cdn.luogu.com.cn/upload/image_hosting/wqd9tpl8.png) There are $2$ Hamiltonian cycles: - $1\to3\to2\to4\to6\to5\to1$; - $1\to5\to4\to6\to3\to2\to1$. #### Explanation for Sample 7 ![](https://cdn.luogu.com.cn/upload/image_hosting/jff0k373.png) There is $1$ Hamiltonian cycle: - $1\to6\to5\to2\to4\to3\to1$. #### Explanation for Sample 8 ![](https://cdn.luogu.com.cn/upload/image_hosting/x2j19zoc.png) There are $12$ Hamiltonian cycles: - $1\to2\to5\to4\to6\to3\to1$; - $1\to2\to5\to6\to3\to4\to1$; - $1\to5\to2\to3\to6\to4\to1$; - $1\to5\to2\to4\to6\to3\to1$; - $1\to5\to4\to6\to2\to3\to1$; - $1\to5\to4\to6\to3\to2\to1$; - $1\to5\to6\to2\to3\to4\to1$; - $1\to5\to6\to3\to2\to4\to1$; - $1\to5\to6\to3\to4\to2\to1$; - $1\to5\to6\to4\to2\to3\to1$; - $1\to6\to3\to2\to5\to4\to1$; - $1\to6\to3\to4\to2\to5\to1$. #### Constraints and Notes For fixed $n$ and $P$, any graph with $m$ edges has weight $\left(\frac{1}{P}\right)^m\left(\frac{P-1}{P}\right)^{n^2-n-m}$. - Subtask 1 (1 pts): the samples. - Subtask 2 (16 pts): $n=15$. - Subtask 3 (20 pts): $n=30$. - Subtask 4 (26 pts): $n=40$. - Subtask 5 (37 pts): $n=50$. Translated by ChatGPT 5