## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

## [Solution] Unordered Swaps Codeforces Solution | Codeforces Problem Solution 2022

E. Unordered Swaps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice had a permutation $p$ of numbers from $1$ to $n$. Alice can swap a pair $\left(x,y\right)$ which means swapping elements at positions $x$ and $y$ in $p$ (i.e. swap ${p}_{x}$ and ${p}_{y}$). Alice recently learned her first sorting algorithm, so she decided to sort her permutation in the minimum number of swaps possible. She wrote down all the swaps in the order in which she performed them to sort the permutation on a piece of paper.

For example,

• $\left[\left(2,3\right),\left(1,3\right)\right]$ is a valid swap sequence by Alice for permutation $p=\left[3,1,2\right]$ whereas $\left[\left(1,3\right),\left(2,3\right)\right]$ is not because it doesn't sort the permutation. Note that we cannot sort the permutation in less than $2$ swaps.
• $\left[\left(1,2\right),\left(2,3\right),\left(2,4\right),\left(2,3\right)\right]$ cannot be a sequence of swaps by Alice for $p=\left[2,1,4,3\right]$ even if it sorts the permutation because $p$ can be sorted in $2$ swaps, for example using the sequence $\left[\left(4,3\right),\left(1,2\right)\right]$.

Input

The first line contains $2$ integers $n$ and $m$ $\left(2\le n\le 2\cdot {10}^{5},1\le m\le n-1\right)$  — the size of permutation and the minimum number of swaps required to sort the permutation.

The next line contains $n$ integers ${p}_{1},{p}_{2},...,{p}_{n}$ ($1\le {p}_{i}\le n$, all ${p}_{i}$ are distinct)  — the elements of $p$. It is guaranteed that $p$ forms a permutation.

Then $m$ lines follow. The $i$-th of the next $m$ lines contains two integers ${x}_{i}$ and ${y}_{i}$  — the $i$-th swap $\left({x}_{i},{y}_{i}\right)$.

It is guaranteed that it is possible to sort $p$ with these $m$ swaps and that there is no way to sort $p$ with less than $m$ swaps.

Output

Print a permutation of $m$ integers  — a valid order of swaps written by Alice that sorts the permutation $p$. See sample explanation for better understanding.

In case of multiple possible answers, output any.