Maximizing Fixed Points

Maximizing Fixed Points

Time Limit: 2.0 s

Memory Limit: 256.0 MB

Description

Mr. Heart is an aspiring stone collector who wants to arrange a collection of enchanted stones numbered from \(1\) to \(N\). Each stone holds a unique number from \(1\) to \(N\), but they are currently in a random order, forming a permutation of the numbers. For instance, a valid permutation could be \(A=[2,1,3]\), which contains all unique numbers from \(1\) to \(N\). In contrast, an invalid arrangement might be \(A=[1,2,2]\), where one number is repeated and another is missing.

Mr. Heart wants to rearrange the stones so that as many stones as possible show their correct number in their rightful position.where the correct position for each stone \(i\) is defined as the index \(i\) in the array \(A\) (where \(1≤i≤N\)). For instance, stone \(1\) should be in position \(1\), stone \(2\) should be in position \(2\), and so on.

To help him achieve this, you will provide Mr. Heart with \(𝑄\) special instructions. Each instruction specifies two positions \((xi,yi)\), where \(xi\) and \(yi\) are positions of the stones in the array \(A\) that can be swapped. This means that if you are given an instruction to swap \(xi\) and \(yi\), you will exchange the values at these two indices in the permutation \(A\).

For example:

  • If \(A=[1,3,2]\) and you receive a swap instruction \((1,2)\), you would swap the elements at positions \(1\) and \(2\), resulting in \(A=[3,1,2]\).

Mr. Heart can use these swaps as many times as needed to maximize the number of stones that match their position.

Input

First line takes an integer \(T\) : number of testcase
first line of each testcase takes two integers \(N\) and \(Q\) : the number of stones and the number of swap instructions.
second line of each testcase takes \(N\) integers \(A1,A2,A3...An\)
The next \(Q\) lines each contain two integers \(xi\) and \(yi\) - the indices of the stones that can be swapped

\(1 <= T <= 100\)
\(2 <= N <= 10^5\)
\(1 <= Q <= 10^5\)
\(1 <= Ai <= N\) - all is a unique value
\(1 <= xi , yi <= N\) and \(xi\)\(yi\)

Sum of \(N\) overall testcase doesn't exceed \(10^5\)

Output

Output a single integer — the maximum number of stones that can be placed in their correct position.

Sample

Input Output
2
3 2
1 3 2
1 2
2 3
3 2
3 2 1
1 2
2 3
3
3

For the test Case \(1\),
Mr. Heart can swap only position \(2\) and \(3\) then A will be = \([1 , 2 , 3]\) , so Answer will be 3.
For the test case \(2\),
Mr. Heart can follow bellow operation for getting maximum Answer,

  1. Swap (\(1\) and \(2\)) = \(A[2 , 3 , 1]\)
  2. Swap (\(2\) and \(3\)) = \(A[2 , 1 , 3]\)
  3. Swap (\(1\) and \(2\)) = \(A[1 , 2 , 3]\)

so answer will be 3

Information

ID
1119
Difficulty
4
Category
(None)
Tags
(None)
# Submissions
32
Accepted
15
Accepted Ratio
47%
Uploaded By