#P4089. Bovine Shuffle Permanency
Bovine Shuffle Permanency
Bovine Shuffle Permanency
Farmer John is convinced that happy cows produce more milk. To ensure this, he has installed a giant disco ball in his barn and plans to teach his cows the Bovine Shuffle!
The cows are initially arranged in positions (1, 2, \ldots, N) (with (1 \leq N \leq 100{,}000)). A single shuffle is defined by a list of (N) integers (a_1, a_2, \ldots, a_N) (each in the range (1 \ldots N)), where a cow in position (i) moves to position (a_i). Note that the (a_i)'s are not necessarily distinct, so if multiple cows arrive at the same position, they merge and will move together in subsequent shuffles.
After an arbitrary number of shuffles, certain positions will always contain cows. Your task is to count the number of such positions. Mathematically, these positions are exactly the nodes that belong to a cycle in the directed graph defined by the function (f(i)=a_i).
inputFormat
The input consists of two lines:
1. The first line contains a single integer (N), the number of cows and positions.
2. The second line contains (N) space-separated integers (a_1, a_2, \ldots, a_N) where each (a_i) ((1 \leq a_i \leq N)) indicates the position to which the cow at position (i) moves during a shuffle.
outputFormat
Output a single integer representing the number of positions that will always contain cows after any number of shuffles (i.e., the number of positions that are part of cycles).
sample
4
3 2 1 3
2
</p>