Double Sort II
Contest: Educational Codeforces Round 141 (Rated for Div. 2)
Difficulty rating: 2500
Tags: dfs and similar, flows, graph matchings, graphs

You are given two permutations $$$a$$$ and $$$b$$$, both of size $$$n$$$. A permutation of size $$$n$$$ is an array of $$$n$$$ elements, where each integer from $$$1$$$ to $$$n$$$ appears exactly once. The elements in each permutation are indexed from $$$1$$$ to $$$n$$$.

You can perform the following operation any number of times:

- choose an integer $$$i$$$ from $$$1$$$ to $$$n$$$;
- let $$$x$$$ be the integer such that $$$a_x = i$$$. Swap $$$a_i$$$ with $$$a_x$$$;
- let $$$y$$$ be the integer such that $$$b_y = i$$$. Swap $$$b_i$$$ with $$$b_y$$$.

Your goal is to make both permutations sorted in ascending order (i. e. the conditions $$$a_1 < a_2 < \dots < a_n$$$ and $$$b_1 < b_2 < \dots < b_n$$$ must be satisfied) using minimum number of operations. Note that both permutations must be sorted after you perform the sequence of operations you have chosen.

Input format:
The first line contains one integer $$$n$$$ ($$$2 \le n \le 3000$$$).

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$; all $$$a_i$$$ are distinct).

The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$; all $$$b_i$$$ are distinct).

Output format:
First, print one integer $$$k$$$ ($$$0 \le k \le 2n$$$) — the minimum number of operations required to sort both permutations. Note that it can be shown that $$$2n$$$ operations are always enough.

Then, print $$$k$$$ integers $$$op_1, op_2, \dots, op_k$$$ ($$$1 \le op_j \le n$$$), where $$$op_j$$$ is the value of $$$i$$$ you choose during the $$$j$$$-th operation.

If there are multiple answers, print any of them.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

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

Example output:
1
2

Example input:
2
1 2
1 2

Example output:
0

Editorial:
The solution to this problem uses cyclic decomposition of permutations. A cyclic decomposition of a permutation is formulated as follows: you treat a permutation as a directed graph on $$$n$$$ vertices, where each vertex $$$i$$$ has an outgoing arc $$$i \rightarrow p_i$$$. This graph consists of several cycles, and the properties of this graph can be helpful when solving permutation-based problems.
First of all, how does the cyclic decomposition of a sorted permutation look? Every vertex belongs to its own cycle formed by a self-loop going from that vertex to itself. We will try to bring the cyclic decompositions of the given permutations to this form.
What does an operation with integer $$$i$$$ do to the cyclic decomposition of the permutation? If $$$i$$$ is in its own separate cycle, the operation does nothing ($$$p_i = i$$$, so we swap an element with itself).
Otherwise, let's suppose that $$$x$$$ is the element before $$$i$$$ in the same cycle ($$$p_x = i$$$), and $$$y$$$ is the element after $$$i$$$ in the same cycle ($$$p_i = y$$$). Note that this can be the same element. When we apply an operation on $$$i$$$, we swap $$$p_x$$$ with $$$p_i$$$, so after the operation, $$$p_i = i$$$, and $$$p_x = y$$$. So, $$$i$$$ leaves the cycle and forms its separate cycle, and $$$y$$$ becomes the next vertex in the cycle after $$$x$$$. So, using the operation, we exclude the vertex $$$i$$$ from the cycle.
Suppose we want to sort one permutation. Then each cycle having length $$$\ge 2$$$ must be broken down: for a cycle of length $$$c$$$, we need to exclude $$$c-1$$$ vertices from it to break it down. The vertex we don't touch can be any vertex from the cycle, and all other vertices from the cycle will be extracted using one operation directed at them. It's easy to see now that if we want to sort a permutation, we don't need to apply the same operation twice, and the order of operations does not matter.
Okay, then what about sorting two permutations in parallel? Let's change the problem a bit: instead of calculating the minimum number of operations, we will try to maximize the number of integers $$$i$$$ such that we don't perform operations with them. So, an integer $$$i$$$ can be left untouched if it is the only untouched vertex in its cycles in both permutations... Can you see where this is going?
Suppose we want to leave the vertex $$$i$$$ untouched. It means that in its cycles in both permutations, every other vertex has to be extracted with an operation. So, if two cycles from different permutations have a vertex in common, we can leave this vertex untouched, as long as there are no other vertices left untouched in both of these cycles. Let's build a bipartite graph, where each vertex in the left part represents a cycle in the first permutation, and each vertex in the right part represents a cycle in the second permutation. We will treat each integer $$$i$$$ as an edge between two respective vertices in the bipartite graph. If the edge corresponding to $$$i$$$ is "used" ($$$i$$$ is left untouched), we cannot "use" any edges incident to the same vertex in left or right part. So, maximizing the number of untouched numbers is actually the same as finding the maximum matching in this bipartite graph.
After you find the maximum matching, restoring the actual answer is easy. Remember that the edges saturated by the matching correspond to the integers we don't touch with our operations, the order of operations does not matter, and each integer has to be used in an operation only once. So, the actual answer is the set of all integers without those which correspond to the edges from the matching.
This solution runs in $$$O(n^2)$$$ even with a straightforward implementation of bipartite matching, since the bipartite graph has at most $$$O(n)$$$ vertices and $$$O(n)$$$ edges.