GAIA Module Installation
Time limit2.5sMemory limit1024 MB
Given permutations p and q, choose for each slot either p_i or q_{p_i} so that no forbidden pair of modules ends up in adjacent slots, or report -1.
Statement
The GAIA system has modules, named MINERVA, AETHER, and others. The modules are numbered from 1 to , and there are exactly slots for connecting them to GAIA. Initially, module is connected to slot .
There are only two operations that rearrange modules across slots. They are described by permutations and of length . Operation moves the module connected to slot into slot . Operation works the same way: the module connected to slot is reconnected to slot .
For GAIA to work correctly, each module must be assigned a slot using a partial composition of and . That means that for each , slot must be connected to either
- the module obtained by applying , or
- the module obtained by applying and then .
In other words, slot can hold module or module . The choice can be made independently for each .
There are also system constraints. Each one says that module cannot be placed in a slot adjacent to module .
Input
The first line contains two integers and (; ), the number of modules and the number of constraints.
The second and third lines contain the elements of permutations and (). Each number from 1 to appears exactly once in each line.
Each of the following lines contains two integers and (; ), the modules that must not be in adjacent slots.
Output
If no valid assignment exists, print .
Otherwise, print integers separated by spaces. The -th integer is 1 if slot uses the assignment from , and 2 if it uses the assignment from .
If several valid assignments exist, print any of them.