Lucky Permutation
Contest: Codeforces Round 842 (Div. 2)
Difficulty rating: 1800
Tags: constructive algorithms, dfs and similar, graphs, greedy

You are given a permutation$$$^\dagger$$$ $$$p$$$ of length $$$n$$$.

In one operation, you can choose two indices $$$1 \le i < j \le n$$$ and swap $$$p_i$$$ with $$$p_j$$$.

Find the minimum number of operations needed to have exactly one inversion$$$^\ddagger$$$ in the permutation.

$$$^\dagger$$$ A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).

$$$^\ddagger$$$ The number of inversions of a permutation $$$p$$$ is the number of pairs of indices $$$(i, j)$$$ such that $$$1 \le i < j \le n$$$ and $$$p_i > p_j$$$.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$).

The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots, p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that $$$p$$$ is a permutation.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case output a single integer — the minimum number of operations needed to have exactly one inversion in the permutation. It can be proven that an answer always exists.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, the permutation already satisfies the condition.

In the second test case, you can perform the operation with $$$(i,j)=(1,2)$$$, after that the permutation will be $$$[2,1]$$$ which has exactly one inversion.

In the third test case, it is not possible to satisfy the condition with less than $$$3$$$ operations. However, if we perform $$$3$$$ operations with $$$(i,j)$$$ being $$$(1,3)$$$,$$$(2,4)$$$, and $$$(3,4)$$$ in that order, the final permutation will be $$$[1, 2, 4, 3]$$$ which has exactly one inversion.

In the fourth test case, you can perform the operation with $$$(i,j)=(2,4)$$$, after that the permutation will be $$$[2,1,3,4]$$$ which has exactly one inversion.

Example input:
4
2
2 1
2
1 2
4
3 4 1 2
4
2 4 3 1

Example output:
0
1
3
1

Editorial:
For some fixed $$$n$$$, there are $$$n - 1$$$ permutations that have exactly $$$1$$$ inversion in them (the inversion is colored):
• $$$\color{red}{2}, \color{red}{1}, 3, 4, \ldots, n - 1, n$$$
• $$$1, \color{red}{3}, \color{red}{2}, 4, \ldots, n - 1, n$$$
• $$$1, 2, \color{red}{4}, \color{red}{3}, \ldots, n - 1, n$$$...
• $$$1, 2, 3, 4, \ldots, \color{red}{n}, \color{red}{n - 1}$$$
Let's build a directed graph with $$$n$$$ vertices where the $$$i$$$-th vertex has an outgoing edge $$$i \rightarrow p_i$$$. It is easy to see that the graph is divided up into cycles of the form $$$i \rightarrow p_i \rightarrow p_{p_i} \rightarrow p_{p_{p_i}} \rightarrow \ldots \rightarrow i$$$.
Let $$$cycles$$$ be the number of cycles in this graph. It is a well know fact that $$$n - cycles$$$ is the minimum number of swaps needed to get the permutation $$$1, 2, 3, \ldots, n$$$ from our initial one (in other words, to sort it).
Suppose we now want to get the $$$k$$$-th permutation from the list above. Let $$$x$$$ and $$$y$$$ be such that $$$p_x = k$$$ and $$$p_y = k + 1$$$.
Let us remove the edges $$$x \rightarrow k$$$ and $$$y \rightarrow k + 1$$$ from the graph and instead add the edges $$$x \rightarrow k + 1$$$ and $$$y \rightarrow k$$$. Let $$$cycles'$$$ be the number of cycles in this new graph. The minimum number of swaps needed to get the $$$k$$$-th permutation in the list is equal to $$$n - cycles'$$$.
Turns out that we can easily calculate $$$cycles'$$$ if we know $$$cycles$$$:
• $$$cycles' = cycles + 1$$$ if the vertices $$$k$$$ and $$$k + 1$$$ were in the same cycle in the initial graph,
• $$$cycles' = cycles - 1$$$ otherwise.
To quickly check if two vertices $$$u$$$ and $$$v$$$ are in the same cycle, assign some id to each cycle (with a simple dfs or with a DSU) and the compare $$$u$$$-s cycle id with $$$v$$$-s cycle id.
The answer is just the minimum possible value of $$$n - cycles'$$$ over all $$$1 \le k \le n - 1$$$.
Time complexity: $$$\mathcal{O}(n)$$$.
PS: you can also find $$$cycles'$$$ with data structures (for example, by maintaining a treap for each cycle).