atcoder#ABC233F. [ABC233F] Swap and Sort

[ABC233F] Swap and Sort

Score : 500500 points

Problem Statement

We have a permutation P=(P1,P2,,PN)P=(P_1,P_2,\ldots,P_N) of (1,2,,N)(1,2,\ldots,N).

There are MM kinds of operations available to you. Operation ii swaps the aia_i-th and bib_i-th elements of PP.

Is it possible to sort PP in ascending order by doing at most 5×1055\times 10^5 operations in total in any order?

If it is possible, give one such sequence of operations. Otherwise, report so.

Constraints

  • 2N10002\leq N \leq 1000
  • PP is a permutation of (1,2,,N)(1,2,\ldots,N).
  • 1Mmin(2×105,N(N1)2)1\leq M \leq \min(2\times 10^5, \frac{N(N-1)}{2})
  • 1ai<biN1\leq a_i \lt b_i\leq N
  • (ai,bi)(aj,bj)(a_i,b_i)\neq (a_j,b_j) if iji\neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

P1P_1 P2P_2 \ldots PNP_N

MM

a1a_1 b1b_1

a2a_2 b2b_2

\vdots

aMa_M bMb_M

Output

If it is possible to sort PP in ascending order, print the following:

KK

c1c_1 c2c_2 \ldots cKc_K

Here, KK represents the number of operations to do, and cic_i (1iK)(1\leq i \leq K) means the ii-th operation to do is Operation cic_i. Note that 0K5×1050\leq K \leq 5\times 10^5 must hold.

If it is impossible to sort PP in ascending order, print -1.

6
5 3 2 4 6 1
4
1 5
5 6
1 2
2 3
3
4 2 1

PP changes as follows: $(5,3,2,4,6,1)\to (5,2,3,4,6,1)\to (5,2,3,4,1,6)\to (1,2,3,4,5,6)$.

5
3 4 1 2 5
2
1 3
2 5
-1

We cannot sort PP in ascending order.

4
1 2 3 4
6
1 2
1 3
1 4
2 3
2 4
3 4
0

PP may already be sorted in ascending order.

Additionally, here is another accepted output:

4
5 5 5 5

Note that it is not required to minimize the number of operations.