Given the move sequence of a shortest round trip that visits every node of an unknown rooted tree, reconstruct each city's parent.
Medium6StackTreeDFSNo attempts yetTime limit1sMemory limit512 MBYunho is a tour guide in Tree Country, where K cities are connected in the shape of a tree. His new package tour starts at the root city of Tree Country, visits every city, and returns to the root. Only the concept of the package is fixed, so Yunho decides the order in which to visit the cities. Eager to finish work quickly, Yunho has been running tours in an order that uses the fewest possible moves among all orders that visit every city and return.
For example, in a Tree Country whose root city is city 0, one visiting order chosen by Yunho is as follows.
0-1-2-1-3-4-3-5-3-1-6-1-0-7-8-7-9-7-0
One day, Yunho lost his map. Fortunately, his tour plan still records the order in which the cities must be visited. He decided to redraw the map from it, but the task was too difficult for him, so you, even more frustrated, decided to report the parent city of every city on his behalf.
For the order above, the corresponding map is expressed by saying that city 0 has no parent, the parent of city 1 is city 0, the parent of city 2 is city 1, and so on.
Input is given from standard input. The first line contains the length N (1≤N≤200,000) of Yunho's visiting order.
The second line contains N integers. The i-th integer Ai is the number of the i-th visited city, and it is guaranteed that there exists a tree that can be visited in the given order. If there are K cities, the cities are numbered from 0 to K−1 with no duplicates. That is, 0≤Ai<K.
Print to standard output. On the first line, print the total number K of cities in Tree Country.
On the second line, print K integers separated by spaces. The i-th number is the parent city of city i, or −1 for the root city, which has no parent.