#P3420. Minimizing Piggy Bank Breakage

    ID: 16675 Type: Default 1000ms 256MiB

Minimizing Piggy Bank Breakage

Minimizing Piggy Bank Breakage

Byteazar the Dragon owns \(N\) piggy banks. Each piggy bank can be opened in one of two ways: by using its corresponding key or by smashing it. Each piggy bank \(i\) contains a key that opens piggy bank \(a_i\). Initially, all piggy banks are locked and the keys are hidden inside them.

To open all piggy banks, Byteazar may first choose to smash some of them to retrieve the keys stored inside. Once a key is retrieved, the corresponding piggy bank can be opened normally. However, if there is a cycle of piggy banks (where each is waiting on a key from another in the cycle), then at least one piggy bank in that cycle must be smashed to break the cycle and retrieve the keys. Thus, the task is to determine the minimum number of piggy banks that have to be smashed so that, eventually, the keys retrieved allow all the piggy banks to be opened.

The problem can be modeled using a directed graph where there is an edge from \(i\) to \(a_i\). The answer equals the number of cycles in this graph.

inputFormat

The first line contains an integer \(N\) (\(1 \le N \le 10^5\)), the number of piggy banks.

The second line contains \(N\) space-separated integers \(a_1, a_2, \dots, a_N\), where each \(a_i\) (\(1 \le a_i \le N\)) indicates that piggy bank \(i\) contains the key for piggy bank \(a_i\).

outputFormat

Output a single integer: the minimum number of piggy banks that must be smashed so that all piggy banks can eventually be opened.

sample

3
1 2 3
3