P17684 [IO 2025 #2] Rostov Permutation
Description
The city government of Rostov-on-Don decided to organize the list of attractions along the Don River. Each attraction has a distinct number (its rating), and the attractions need to be arranged in increasing order of their ratings.
However, moving attractions is not easy! Two attractions can be swapped only between two positions where a swap is technically allowed. The pairs of positions where swaps are allowed are determined by the city's infrastructure network.
Formally, you are given an array $a$ of $n$ distinct integers. The swap operation `swap`, which exchanges $a_i$ and $a_j$, can be performed only if the pair of positions $(i,j)$ appears in the list of allowed swap pairs.
Determine whether the array can be sorted in increasing order using only these allowed swaps. If it can, you also need to provide a sequence of swap operations satisfying the output limits below; the number of operations does not need to be minimal.
Input Format
The first line contains an integer $n$ ($1\le n\le2000$), the number of attractions.
The second line contains $n$ distinct integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le10^9$), the ratings of the attractions.
The third line contains an integer $m$ ($0\le m\le\frac{n(n-1)}2$), the number of allowed swap pairs.
Each of the next $m$ lines contains two integers $u_i,v_i$ ($1\le u_i,v_i\le n$, $u_i\ne v_i$), meaning that the attractions at positions $u_i$ and $v_i$ can be swapped.
The pairs are undirected: $(u,v)$ and $(v,u)$ denote the same swap.
Output Format
If the array can be sorted in increasing order of ratings using only the allowed swaps, output `YES`; otherwise output `NO`.
If you output `YES`, print the number of operations $q$ ($0\le q\le4\cdot10^6$) on the next line, then print $q$ integers on the following line, giving the numbers of the swap operations to perform in order. Each operation number refers to one allowed swap pair in the input; the pairs are numbered starting from $1$ in input order. If $q=0$, the operation list is empty.
Explanation/Hint
Translated by QwenWork