Allowed Swaps
시간 제한2초메모리 제한512 MB
주어진 교환 목록에 있는 위치끼리만 바꿔서 순열을 정렬하고, 불가능하면 -1을 출력한다. 교환 횟수는 500000 이하이면 된다.
문제
You are given a permutation of length and a list of allowed swaps. Each swap is defined by two distinct integers between 1 and , inclusive --- positions in the permutation that can be swapped.
Your task is to sort the permutation using no more than allowed swaps or determine that it is impossible. Note that each allowed swap can be used more than once.
입력
The first line contains a single integer --- the length of the permutation ().
The second line contains distinct integers between 1 and , inclusive --- the permutation itself.
The third line contains a single integer () --- the number of allowed swaps. Then lines follow, each containing two integers and (; ), denoting that elements on positions and can be swapped.
You may assume that no two allowed swaps coincide.
출력
If it is impossible to sort the array using no more than swaps from the given list, print .
Otherwise, print the number of swaps used, , followed by pairs of integers and (; ) describing the sequence of swaps. Each pair must belong to the list of allowed swaps (formally, for each , there must exist such that either and , or and ).
If there is more than one solution, print any of them. The number of swaps does not have to be minimized.