[Solution] Unordered Swaps Codeforces Solution | Codeforces Problem Solution 2022
Alice had a permutation of numbers from to . Alice can swap a pair which means swapping elements at positions and in (i.e. swap and ). 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,
- is a valid swap sequence by Alice for permutation whereas is not because it doesn't sort the permutation. Note that we cannot sort the permutation in less than swaps.
- cannot be a sequence of swaps by Alice for even if it sorts the permutation because can be sorted in swaps, for example using the sequence .
The first line contains integers and — the size of permutation and the minimum number of swaps required to sort the permutation.
The next line contains integers (, all are distinct) — the elements of . It is guaranteed that forms a permutation.
Then lines follow. The -th of the next lines contains two integers and — the -th swap .
It is guaranteed that it is possible to sort with these swaps and that there is no way to sort with less than swaps.
Print a permutation of integers — a valid order of swaps written by Alice that sorts the permutation . See sample explanation for better understanding.
In case of multiple possible answers, output any.
No comments:
Post a Comment