The Bovine Shuffle
InterviewTime limit2sMemory limit512 MB
Given a functional graph where each position i sends its cow to a_i, count the positions that hold at least one cow no matter how many shuffles are applied.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Implementation, Simulation
- Solved
- No attempts yet
Problem
Farmer John is convinced that happy cows give more milk, so he has hung a giant disco ball in his barn and wants to teach his cows to dance.
After looking up popular cow dances, Farmer John settles on the Bovine Shuffle. In the Bovine Shuffle his cows () line up in a row and then perform one shuffle after another, and each shuffle can reorder the line. So the cows can find their own spots, Farmer John numbers the places in the row from to . At the start each place holds exactly one cow.
One shuffle is described by numbers . When a shuffle happens, every cow standing at place moves to place . Each is between and . All cows move to their new places at the same time. The values are not guaranteed to be distinct, so several cows can end up at the same place, and once cows share a place they move together for all remaining shuffles.
Farmer John notices that some places hold cows no matter how many shuffles take place. Count how many such places there are.
Input
The first line contains , the number of cows.
The second line contains the integers separated by spaces.
Output
Print on one line the number of places that always hold cows, no matter how many shuffles take place.