P16271 [Lanqiao Cup 2026 NOI Qualifier Java B Group] Arena Match
Description
Xiao Lan wants to organize an arena match. There are $n$ players, numbered from $1$ to $n$. Initially, each player $i$ is on their own arena $i$. Each player $i$ has exactly one target arena $a_i$ that they want to go to and challenge.
Before the match starts, Xiao Lan needs to decide an order in which the players will act. Following this order, for each player $i$ who acts:
- If their target arena $a_i$ has not been closed yet, they will leave their own arena $i$ and go to arena $a_i$ to challenge, and at the same time close their original arena $i$.
- If their target arena $a_i$ has already been closed, then they cannot start the challenge and can only stay where they are.
Xiao Lan wants to choose an order so that in the end, the number of arenas that have been used (i.e., successfully visited as a challenge target) is as small as possible. Note that players who stay where they are do not count as using an arena.
Please help Xiao Lan compute: under the best acting order, what is the minimum number of arenas that will be used?
Input Format
The input has 2 lines.
The first line contains a positive integer $n$, representing the number of players.
The second line contains $n$ positive integers $a_1, a_2, \dots, a_n$, representing the target arena number for each player.
Output Format
Output one integer, representing the minimum number of arenas that have been used.
Explanation/Hint
### Sample Explanation
One optimal acting order is: $1, 3, 2, 5, 4$.
1. Player $1$ challenges $2$: target arena $2$ is not closed, the action succeeds, arena $1$ is closed, arena $2$ is used;
2. Player $3$ challenges $4$: target arena $4$ is not closed, the action succeeds, arena $3$ is closed, arena $4$ is used;
3. Player $2$ challenges $3$: target arena $3$ was closed when player $3$ acted, so player $2$ can only stay where they are;
4. Player $5$ challenges $4$: target arena $4$ is not closed, the action succeeds, arena $5$ is closed, arena $4$ is used;
5. Player $4$ challenges $5$: target arena $5$ was closed when player $5$ acted, so player $4$ can only stay where they are.
In the end, the arenas that have been used are $\{2, 4\}$, a total of $2$.
### Constraints and Notes
For $30\%$ of the testdata, $n \leq 10$.
For $100\%$ of the testdata, $1 \leq n \leq 10^6$, $1 \leq a_i \leq n$, and $a_i \ne i$.
Translated by ChatGPT 5