#P14116. [IAMOI R4] 序列

    ID: 13574 远端评测题 3000ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>图论贪心洛谷原创O2优化图论建模基环树洛谷月赛

[IAMOI R4] 序列

Description

Little T has two sequences, aa and bb, both of length nn. Some elements in sequence aa are determined, while the rest are undetermined. Little T must fill in the undetermined elements. All elements in sequence aa must be integers between 11 and nn, inclusive.

After Little T determines all elements of sequence aa, an operation is performed mm times. Each operation consists of two steps:

  1. For all i[1,n]i \in [1, n], set biaib_i \gets a_i.
  2. For all i[1,n]i \in [1, n], set aibbia_i \gets b_{b_i}.

Little T wants to know, after all mm operations are completed, what is the maximum possible number of distinct elements in sequence aa?

Input Format

This problem has multiple test cases.

The first line of the input contains an integer TT, representing the number of test cases.

Then, TT test cases follow. For each test case:

  • The first line contains two positive integers nn and mm, representing the length of the sequences and the number of operations.
  • The second line contains nn integers, representing the initial sequence aa. If ai=0a_i = 0, the element at this position is undetermined. Otherwise, the element is determined.

Output Format

For each test case, output a single line containing a positive integer, which is the answer.

3
6 2
1 1 4 5 1 4
6 2
0 0 4 5 0 4
13 1
0 1 2 3 4 5 2 7 8 3 10 4 12
1
5
7

Hint

【Sample Explanation】

For the first sample case, the initial sequence aa is fully determined. After 2 operations, sequence aa becomes 1 1 1 1 1 1. The number of distinct elements is 1.

For the second sample case, Little T can set the initial sequence aa to 2 1 4 5 6 4. After 2 operations, sequence aa becomes 1 2 4 5 6 4. The number of distinct elements is 5.

【Constraints】

This problem uses bundled testing.

Subtask nn \le mm Special Property Score
1 88 8\le 8 None 10
2 50005000 5000\le 5000 15
3 10510^5 =109=10^9 10
4 109\le 10^9 Yes
5 None 15
6 10610^6 =109=10^9 10
7 109\le 10^9 Yes
8 None 20
  • Special Property: For all i[1,n]i \in [1, n], ai0a_i \neq 0.

For all test cases, it is guaranteed that: 1T51 \le T \le 5, 1n1061 \le n \le 10^6, 1m1091 \le m \le 10^9, 0ain0 \le a_i \le n.

【Hints】

The input size may be large. Please use fast I/O methods. Please note the specific time and memory limits for this problem.