Toy Marbles

시간 제한2초메모리 제한256 MB

요약
각 컨테이너에 구슬이 하나씩 들어 있을 때, 교환과 이동만으로 모든 구슬을 제 색 컨테이너로 옮기는 최소 동작 순서를 구한다.
난이도

보통10점 중 7점

유형
그래프, 그리디, 시뮬레이션, 구현
정답자
아직 제출이 없습니다

문제

Busy Beaver has discovered that someone has mixed up his toy marbles!

There are NN containers, numbered from 11 to NN. The ii-th container currently contains a single marble of color c_ic\_i.

Busy Beaver wants to tidy up his marbles so that the ii-th container only contains marbles of color ii. To achieve this, he can perform either of the following actions any number of times (possibly zero):

  • Swap the marbles in two containers xx and yy. After this action, all marbles from container xx move to container yy, and vice versa.
  • Move all the marbles from one container yy to another container xx. After this action, container yy becomes empty, and all its marbles are moved to container xx.

Find a way to organize the marbles using the minimum number of actions.

입력

The first line contains an integer NN (1≤N≤2⋅1051\leq N\leq 2\cdot 10^5) — the number of containers.

The second line contains NN integers c_1,c_2,…,c_Nc\_1,c\_2,\dots ,c\_N (1≤c_i≤N1\leq c\_i\leq N) — the marble initially in each container.

출력

On the first line, print a single integer KK — the minimum number of actions required.

On the next KK lines, describe the actions in order, one per line. Each action should be in one of the following formats:

  • 1 xx yy: Swap the marbles in containers xx and yy (1≤x,y≤N1\leq x,y\leq N; x≠yx\neq y).
  • 2 xx yy: Move all marbles from container yy to container xx (1≤x,y≤N1\leq x,y\leq N; x≠yx\neq y).

If there are multiple ways to achieve the goal in the minimum number of actions, you may print any valid solution.

예제2

  1. 예제 1

    입력
    4
    2 4 3 1
    
    예상 출력
    2
    1 2 4
    1 1 2
    
  2. 예제 2

    입력
    8
    3 6 7 6 3 6 8 3
    
    예상 출력
    6
    2 6 4
    2 6 2
    1 8 7
    1 7 3
    2 3 1
    2 3 5