Commuting Functions
Time limit3sMemory limit256 MB
Given a permutation f on {1..n}, construct the lexicographically smallest permutation g that commutes with f under composition.
Statement
Two functions and () commute if and only if for every . For example, and commute, whereas and do not.
Every function (, where and is a positive integer) can be written as a value list — a list whose -th element equals . For example, the function from to has the value list .
Value lists are ordered lexicographically: the list is smaller than the list if and only if there is an index with and for every index .
A function () is bijective if for every there is exactly one with .
Given a bijective function (), find the function that commutes with and has the lexicographically smallest possible value list.
Input
The first line contains a single integer — the number of elements in the value list of the bijective function ().
The second line contains the value list of : integers that form a permutation of .
Output
Print a single line with integers — the value list of the function that commutes with and has the lexicographically smallest value list.