atcoder#AGC060E. [AGC060E] Number of Cycles

[AGC060E] Number of Cycles

Score : 14001400 points

Problem Statement

In this problem, a permutation of (1,2,,N)(1,2,\cdots,N) is occasionally called just a permutation.

For a permutation a=(a1,a2,,aN)a=(a_1,a_2,\cdots,a_N), let f(a)f(a) be the number of cycles in aa. More accurately, the value of f(a)f(a) is defined as follows.

  • Consider an undirected graph consisting of NN vertices numbered 11 to NN. For each 1iN1 \leq i \leq N, add an edge connecting vertex ii and vertex aia_i. Then, let f(a)f(a) be the number of connected components in this graph.

You are given a permutation P=(P1,P2,,PN)P=(P_1,P_2,\cdots,P_N) and an integer KK. Determine whether there is a permutation xx that satisfies the following condition, and construct one if it exists.

  • Let yi=Pxiy_i=P_{x_i} to define a permutation yy.
  • Then, f(x)+f(y)=Kf(x)+f(y)=K holds.

For each input file, solve TT test cases.

Constraints

  • 1T1051 \leq T \leq 10^5
  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 2K2N2 \leq K \leq 2N
  • (P1,P2,,PN)(P_1,P_2,\cdots,P_N) is a permutation of (1,2,,N)(1,2,\cdots,N).
  • In each input file, the sum of NN does not exceed 2×1052 \times 10^5.
  • All numbers in the input are integers.

Input

The input is given from Standard Input in the following format:

TT

case1case_1

case2case_2

\vdots

caseTcase_T

Each case is in the following format:

NN KK

P1P_1 P2P_2 \cdots PNP_N

Output

For each case, if no permutation xx satisfies the condition, print No. Otherwise, print your answer in the following format:

Yes

x1x_1 x2x_2 \cdots xNx_N

Each character in Yes or No in the output may be either uppercase or lowercase. If multiple solutions exist, any of them will be accepted.

3
3 3
1 3 2
2 2
2 1
4 8
1 2 3 4
Yes
2 1 3
No
Yes
1 2 3 4

In the first case, letting x=(2,1,3)x=(2,1,3) makes y=(3,1,2)y=(3,1,2), satisfying f(x)+f(y)=2+1=3f(x)+f(y)=2+1=3.