Yet Another Problem About Permutations

아직 제출이 없습니다시간 제한3초메모리 제한256 MB

문제

This is a problem about permutations. If you are not familiar with some of the terms used below, please see the note following the example.

A permutation pp is said to be simple if the length of each of its cycles does not exceed two. For example, permutation 2,1,4,32, 1, 4, 3 is simple, but permutation 3,1,23, 1, 2 is not.

You are given a permutation pp. Your task is to represent it as a product of minimal number of simple permutations.

입력

The first line of input contains one integer TT, the number of test cases (1T1051 \le T \le 10^5). The test cases follow.

Each of next TT lines describes a single test case. Each test case description consists of an integer nn, the length of the permutation pp (1n1051 \le n \le 10^5), followed by nn distinct integers p_1,p_2,,p_np\_1, p\_2, \ldots, p\_n, the permutation pp itself (1p_in1 \le p\_i \le n, each number from 11 to nn appears in the permutation exactly once).

The total length of all permutations in the input is not greater than 10610^6.

출력

For each test case, start by printing a line containing an integer kk, the minimal number of simple permutations in the product. The next kk lines must describe simple permutations q(1)q^{(1)}, q(2)q^{(2)}, \ldots, q(k)q^{(k)}, one per line. On ii-th of these lines, print nn distinct integers from 11 to nn describing permutation q(i)q^{(i)}. The product q(1)q(2)q(k)q^{(1)} \circ q^{(2)} \circ \ldots \circ q^{(k)} must be equal to pp.

If there are several optimal answers, print any one of them.

힌트

A permutation of length nn is a sequence of nn integers where each integer from 11 to nn appears exactly once.

A cycle in a permutation pp is a sequence i_1,i_2,,i_ti\_1, i\_2, \ldots, i\_t of distinct integers from 11 to nn such that p_i_1=i_2p\_{i\_1} = i\_2, p_i_2=i_3p\_{i\_2} = i\_3, \ldots, p_i_t1=i_tp\_{i\_{t - 1}} = i\_t and p_i_t=i_1p\_{i\_t} = i\_1. The number t1t \ge 1 is called the length of the cycle.

The product aba \circ b of two permutations aa and bb is a permutation cc such that for each ii, c_i=a_b_ic\_i = a\_{b\_i}. For example, if a=3,2,1a = 3 \\, 2 \\, 1 and b=1,3,2b = 1 \\, 3 \\, 2, their product is ab=3,1,2a \circ b = 3 \\, 1 \\, 2.

The product of three or more permutations can be evaluated in any order, for example, abc=(ab)c=a(bc)a \circ b \circ c = (a \circ b) \circ c = a \circ (b \circ c).