QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#8730. Partition

Statistics

A partition of the set $\{1, 2, \dots, N\}$ (for a given natural number $N$) is any collection of non-empty sets such that every number from $1$ to $N$ appears in exactly one of those sets. For example, one partition of the set $\{1, 2, 3, 4, 5\}$ consists of the sets $\{1, 3\}$, $\{2, 4\}$, and $\{5\}$. One way we can define a partition is by using a sequence of numbers $x_1, x_2, \dots, x_N$ ($1 \le x_i \le N$) such that we declare that $i$ and $j$ are in the same set of the partition if and only if $x_i = x_j$. The partition from the previous example could have been defined by the sequence $1, 2, 1, 2, 3$, but also by a sequence like $5, 1, 5, 1, 4$.

Patricija is a girl who owns two partitions of the set $\{1, 2, \dots, N\}$. The first of these partitions is defined by the sequence $a_1, a_2, \dots, a_N$, and the second by the sequence $b_1, b_2, \dots, b_N$. Patricija is interested in the answer to the following question: what is the minimum number of sets that form a partition of the set $\{1, 2, \dots, N\}$, if she has the sets of the two given partitions at her disposal?

Depending on the given number $k \in \{0, 1, 2\}$, it is necessary to do the following:

  • If $k = 0$, it is necessary to find the answer to Patricija's question.
  • If $k = 1$, it is allowed to change at most one of the given $2N$ numbers that define the partitions. It is necessary to find the minimum possible answer to Patricija's question after at most one change. In other words, it is necessary to minimize the minimum number of sets that form a partition.
  • If $k = 2$, it is allowed to change at most one of the given $2N$ numbers that define the partitions. It is necessary to find the maximum possible answer to Patricija's question after at most one change. In other words, it is necessary to maximize the minimum number of sets that form a partition.

Note that when $k = 1$ or $k = 2$, the new value of the changed number must be between $1$ and $N$.

Help Patricija and write a program that solves $T$ such test cases.

Input

The first line contains the numbers $T$ and $k$, the number of test cases and the parameter that determines the type of task, respectively. The descriptions of $T$ test cases follow. Each test case starts with a natural number $N$, the size of the partition. The next two lines contain the sequences $a_1, \dots, a_N$ and $b_1, \dots, b_N$ that define the partitions.

Output

For each of the $T$ test cases, print the answer to the requested question on a separate line.

Subtasks

Subtask Points Constraints
1 7 $k = 0, N \le 8$, the sum of $2^N$ over all test cases is $\le 10^5$.
2 23 $k = 0$
3 15 $k = 1, N \le 1000$, the sum of $N^2$ over all test cases is $\le 10^6$.
4 16 $k = 1$
5 19 $k = 2, N \le 1000$, the sum of $N^2$ over all test cases is $\le 10^6$.
6 20 $k = 2$

In all subtasks, $1 \le T \le 200\,000$, $0 \le k \le 2$, and $1 \le a_i, b_i \le N$ for all $i = 1, 2, \dots, N$. The sum of the values of $N$ over all test cases is at most $200\,000$.

Examples

Input 1

2 0
4
1 1 2 3
1 2 3 3
7
1 1 1 1 2 3 4
1 2 3 4 4 4 4

Output 1

2
4

Input 2

3 1
4
1 1 2 3
1 2 3 3
4
1 1 1 1
1 2 3 3
7
1 1 1 1 2 3 4
1 2 3 4 4 4 4

Output 2

2
1
2

Input 3

3 2
4
1 1 2 3
1 2 3 3
4
1 1 1 3
1 2 3 3
7
1 1 1 2 3 4 5
1 2 3 4 4 4 4

Output 3

3
3
4

Note

Explanation of the first example: For the first test case: The first sequence defines a partition into sets $\{1, 2\}$, $\{3\}$, and $\{4\}$, and the second into sets $\{1\}$, $\{2\}$, and $\{3, 4\}$. Using these sets, we can create a partition into two sets $\{1, 2\}$ and $\{3, 4\}$.

For the second test case: The first sequence defines a partition into sets $\{1, 2, 3, 4\}$, $\{5\}$, $\{6\}$, and $\{7\}$, and the second into sets $\{1\}, \{2\}, \{3\}$, and $\{4, 5, 6, 7\}$. Either of these partitions is also optimal.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.