Module: Topological sort


Problem

3 /5


Lexicographically minimal topological sort

Problem

You are given a connected acyclic directed graph. Find its lexicographically minimal topological sort.
 
Input
The first line contains the number of vertices n (1 <= n <= 10000). The second line contains n numbers ai (0 <= ai <= n, ai != i). The value ai is the ancestor of the vertex with the number i (vertices are numbered from 1). If a< sub>i = 0, then the vertex i is a root and has no ancestors, it is guaranteed that there are exactly 1 such vertices.
 
Output
The solution should output n numbers - the lexicographically minimal topological sort.
 
Examples
# Input Output
1
4
2 0 1 2
2 1 3 4