#C3546. Relay Race Order
Relay Race Order
Relay Race Order
You are given a relay race scenario involving N team members. The race always starts with runner 1. Each runner passes the baton to another runner as specified by a list P. Here, each element Pi represents the position of the runner (using 1-based indexing) who will receive the baton from the ith runner.
Your task is to compute the order in which the team members run the relay race. The order is determined by starting at runner 1, and then repeatedly looking up the next runner from the list P until all runners have run. Mathematically, if we denote the running order by an array order, the process is described by:
\[ \text{order}[i] = \text{currentRunner}, \quad \text{currentRunner} = P_{\text{currentRunner}}, \quad \text{for } i = 0, 1, \ldots, N-1 \]
Note that the indexing in the input list P
is 1-based. Ensure your solution properly translates this into 0-based indexing when accessing array elements.
inputFormat
The input is given via standard input (stdin).\n\nThe first line contains an integer (N) - the number of team members.\nThe second line contains (N) space-separated integers (P_1, P_2, \ldots, P_N), where (P_i) indicates the position of the runner who receives the baton from runner (i).
outputFormat
Output the running order of the team members as (N) space-separated integers on a single line via standard output (stdout).## sample
4
2 3 4 1
1 2 3 4
</p>