luogu#P8281. 「MCOI-08」Fast Enumeration
「MCOI-08」Fast Enumeration
Problem Description
Technoblade abstracts Skyblock as a simple directed graph with nodes () and edges. He needs to find all Hamiltonian cycles in this graph, i.e., all permutations such that and is a valid path.
The testdata guarantees that the number of Hamiltonian cycles is non-zero and less than .
The testdata is sampled randomly from all valid inputs.
Input Format
The first line contains two positive integers .
The next lines each contain two positive integers .
Output Format
Output several lines. Each line outputs positive integers, representing one Hamiltonian cycle. Output them in increasing lexicographical order.
3 3
1 2
2 3
3 1
1 2 3
4 6
1 3
1 4
2 1
2 3
3 4
4 2
1 3 4 2
5 8
1 2
2 3
3 4
4 1
2 5
4 5
5 1
5 3
1 2 3 4 5
1 2 5 3 4
5 10
1 2
1 3
2 3
2 4
3 4
3 5
4 1
4 5
5 1
5 2
1 2 3 4 5
1 3 5 2 4
6 15
1 3
1 4
1 5
2 1
2 3
2 4
3 1
3 4
4 2
4 6
5 2
5 3
6 1
6 2
6 3
1 5 2 3 4 6
1 5 2 4 6 3
1 5 3 4 6 2
6 15
1 3
1 5
2 1
2 4
3 1
3 2
3 4
3 5
3 6
4 6
5 1
5 4
5 6
6 3
6 5
1 3 2 4 6 5
1 5 4 6 3 2
6 16
1 3
1 6
2 3
2 4
2 6
3 1
3 6
4 2
4 3
4 5
4 6
5 2
5 6
6 1
6 3
6 5
1 6 5 2 4 3
6 21
1 2
1 5
1 6
2 1
2 3
2 4
2 5
3 1
3 2
3 4
3 6
4 1
4 2
4 6
5 1
5 2
5 4
5 6
6 2
6 3
6 4
1 2 5 4 6 3
1 2 5 6 3 4
1 5 2 3 6 4
1 5 2 4 6 3
1 5 4 6 2 3
1 5 4 6 3 2
1 5 6 2 3 4
1 5 6 3 2 4
1 5 6 3 4 2
1 5 6 4 2 3
1 6 3 2 5 4
1 6 3 4 2 5
Hint
Explanation for Sample 1

There is Hamiltonian cycle:
- .
Explanation for Sample 2

There is Hamiltonian cycle:
- .
Explanation for Sample 3

There are Hamiltonian cycles:
- ;
- .
Explanation for Sample 4

There are Hamiltonian cycles:
- ;
- .
Explanation for Sample 5

There are Hamiltonian cycles:
- ;
- ;
- .
Explanation for Sample 6

There are Hamiltonian cycles:
- ;
- .
Explanation for Sample 7

There is Hamiltonian cycle:
- .
Explanation for Sample 8

There are Hamiltonian cycles:
- ;
- ;
- ;
- ;
- ;
- ;
- ;
- ;
- ;
- ;
- ;
- .
Constraints and Notes
For fixed and , any graph with 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): .
- Subtask 3 (20 pts): .
- Subtask 4 (26 pts): .
- Subtask 5 (37 pts): .
Translated by ChatGPT 5