Hamilton Path
InterviewTime limit4sMemory limit512 MB
Count the permutations whose consecutive pairs are exactly the directed edges of the graph, with no other edges, and list their values when there are at most n.
- Level
Easy3 of 10
- Topics
- Graph, Implementation
- Solved
- No attempts yet
Problem
A directed graph has vertices and edges. The vertices are labeled from to . Find all permutations of the vertices that satisfy the following constraint.
- For all , the edge exists if and only if .
The value of a permutation is .
Output the number of such permutations modulo . If the number of such permutations is at most , also list their values in the lexicographical order of the permutations.
Input
The first line contains an integer (), the number of test cases.
For each test case, the first line contains two integers and (, , , ). Each of the following lines contains two integers and (, ), meaning there is a directed edge from to . The graph may contain parallel edges.
Output
For each test case, print the number of permutations modulo on the first line. If the number of permutations is at most , print another line with the values of all the permutations, separated by spaces, in lexicographical order of the permutations. If the number is greater than or there is no solution, you do not need to print the empty line.