The Lost Cows
Time limit1sMemory limit128 MB
Given a synchronizing automaton over N states with M shared input letters, compute the maximum over all pairs of the shortest synchronizing word length for that pair.
- Level
Hard8 of 10
- Topics
- BFS, Graph, Shortest path, Implementation
- Solved
- No attempts yet
Problem
On a sunny day, farmer John was kidnapped by the cows of his neighbor, farmer Marcus. The forced holiday does not bother him, but he wants to be sure that his own cows can end up together.
John's pastures are numbered through , and one cow stands in each pasture. The barn is pasture . Every pasture has signs, and sign at pasture points along a path to pasture . A sign sometimes points back to the pasture it stands in.
Marcus's cows let John write one list of instructions and hand it to all of his cows. An instruction is a single sign number. A cow reads the first number on the list, finds that sign in the pasture where she stands, and walks the path it points to. In the new pasture she reads the second number on the list and walks again the same way. She repeats this until the list runs out. Every cow follows the same list in the same order, so one instruction moves every cow one step.
John starts by working out how many instructions it takes to bring two cows into one pasture. For pastures and , let be the smallest length of a list such that the cow starting at and the cow starting at stand in the same pasture after the whole list has been followed. The two cows may be in different pastures along the way; only their positions after the last instruction have to match.
A list that gathers every cow at the barn is guaranteed to exist, so is defined for every pair , .
Input
The first line contains the number of pastures and the number of signs (, ).
Line of the next lines contains integers (), where is the pasture that sign at pasture points to.
Output
Print the largest value of over all pairs of pastures .