시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 128 MB 12 1 1 50.000%

문제

어랜지는 창영이가 즐겨하는 플래시 게임이다. 이 게임은 1부터 N까지 숫자로 이루어진 순열이 주어졌을 때, 숫자의 위치를 적절히 바꿔서 1,2,3,...,N 순서로 만드는 게임이다.

하지만, 모든 숫자의 위치를 바꿀 수 있는 것은 아니다. 플레이어는 미리 정해져있는 교환만 사용할 수 있다.

창영이는 최고 기록을 깨려고 한다. 따라서, 교환을 되도록 적게 사용하려고 한다.

순열의 순서와 사용할 수 있는 교환의 위치가 주어졌을 때, 교환을 되도록 적게 사용해서 오름차순으로 만드는 프로그램을 작성하시오.

입력

첫째 줄에 순열의 길이 N과 사용할 수 있는 교환의 수 M이 주어진다. (1 ≤ N ≤ 12, 1 ≤ M ≤ N*(N-1)/2)

둘째 줄에는 1부터 N까지 숫자로 이루어져 있는 순열이 주어진다.

셋째 줄부터 M개 줄에는 사용할 수 있는 교환의 방법이 주어진다. 방법은 두 숫자 A와 B로 이루어져 있고, A번째 위치와 B번째 위치의 수를 서로 바꾸는 교환을 사용할 수 있다는 뜻이다. 동일한 교환이 두 개 이상 주어지지 않는다.

출력

첫째 줄에 사용한 교환의 수 X를 출력한다. 다음 줄부터 사용한 교환 번호를 한 줄에 하나씩 출력한다. 입력의 첫 번째로 주어지는 교환이 1번이고, 나머지는 순서대로 증가한다. 항상 답이 존재하는 경우만 입력으로 주어지며, 방법이 여러가지일 때는 아무거나 출력하면 된다.

예제 입력 1

3 2
2 1 3
1 3
2 3

예제 출력 1

3
2
1
2

힌트

W3sicHJvYmxlbV9pZCI6IjI5MTgiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJBcnJhbmdlIiwiZGVzY3JpcHRpb24iOiJcclxuPHA+XHJcblx0XHVjNWI0XHViNzljXHVjOWMwXHViMjk0IFx1Y2MzZFx1YzYwMVx1Yzc3NFx1YWMwMCBcdWM5OTBcdWFjYThcdWQ1NThcdWIyOTQgX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