Logo
(追記) (追記ここまで)

18919번 - Allowed Swaps 스페셜 저지다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB96252536.765%

문제

You are given a permutation of length $N$ and a list of allowed swaps. Each swap is defined by two distinct integers between 1 and $N,ドル inclusive --- positions in the permutation that can be swapped.

Your task is to sort the permutation using no more than 5ドル \cdot 10^5$ 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 $N$ --- the length of the permutation (3ドル \le N \le 1000$).

The second line contains $N$ distinct integers between 1 and $N,ドル inclusive --- the permutation itself.

The third line contains a single integer $S$ (1ドル \le S \le 2 \cdot 10^5$) --- the number of allowed swaps. Then $S$ lines follow, each containing two integers $p_i$ and $q_i$ (1ドル \le p_i, q_i \le N$; $p_i \ne q_i$), denoting that elements on positions $p_i$ and $q_i$ can be swapped.

You may assume that no two allowed swaps coincide.

출력

If it is impossible to sort the array using no more than 5ドル \cdot 10^5$ swaps from the given list, print $-1$.

Otherwise, print the number of swaps used, $K,ドル followed by $K$ pairs of integers $x_j$ and $y_j$ (1ドル \le x_j, y_j \le N$; $x_j \ne y_j$) describing the sequence of swaps. Each pair must belong to the list of allowed swaps (formally, for each $j,ドル there must exist $i$ such that either $x_j = p_i$ and $y_j = q_i,ドル or $x_j = q_i$ and $y_j = p_i$).

If there is more than one solution, print any of them. The number of swaps does not have to be minimized.

제한

예제 입력 1

4
4 1 2 3
4
1 2
2 3
3 4
4 1

예제 출력 1

3
4 1
1 2
2 3

예제 입력 2

4
1 3 4 2
2
2 1
2 3

예제 출력 2

-1

힌트

출처

Camp > Bytedance-Moscow Workshops Camp > Bytedance-Moscow Workshops Camp 2020 > Contest 1 A번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /