#P3651. Satellite Relay Troubles

    ID: 16902 Type: Default 1000ms 256MiB

Satellite Relay Troubles

Satellite Relay Troubles

There are $N$ relay satellites numbered from 1 to $N$. Each satellite i is currently configured to receive data from satellite $A_i$ (called its source). Each satellite can only receive data from one other satellite, and this reception link is directed.

Due to some issues, the reception sources of the satellites can be modified; however, each modification incurs a cost. In order to achieve proper relaying, it is required that every pair of satellites can communicate with each other (directly or indirectly) — that is, the undirected graph formed by the satellites and the links (ignoring the direction) must be connected.

Assume that changing the reception source of any satellite costs 1 unit. Compute the minimum total cost needed to reconfigure the network so that the undirected underlying graph is connected.

Formally, let the current configuration be represented as directed edges from i to $A_i$ for i = 1, 2, \dots, N. You are allowed to change some of these edges; each change will cost 1 unit. After all modifications, the undirected graph (where an edge between i and $A_i$ is considered without direction) must be connected. Determine the minimum cost required.

Hint: In the initial configuration the undirected graph consists of several connected components. To connect k components, you need at least k-1 modifications.

The formulas are in \(\LaTeX\) format (e.g. \(N\) and \(A_i\)).

inputFormat

The first line contains an integer N \( (1 \leq N \leq 10^5) \) — the number of satellites.

The second line contains \(N\) integers \(A_1, A_2, \dots, A_N\) \( (1 \leq A_i \leq N) \) where \(A_i\) denotes the satellite from which the i-th satellite receives data.

outputFormat

Output a single integer — the minimum cost (number of modifications) required to make the network connected.

sample

3
1 2 3
2