Problem
Input format
The first line contains two natural numbers `n` and `x`, separated by a space. The second line contains the `n` values of the parent array, separated by spaces. - `1 <= n <= 100` - `1 <= x <= n` - the array describes a valid tree - if `x` is the root, print only `x`
Output format
The program prints the indices of the nodes on the path from `x` to the root, separated by spaces, in order: `x`, parent of `x`, grandparent of `x`, ..., root.
Example
7 1 2 4 4 0 4 5 5
1 2 4
Stuck?
Use the Get Hint button in the action bar to reveal a guided hint.
💬 Discussion
Loading...No comments yet. Be the first to start the discussion!
Sample cases (from the problem)
Sample Case 1
7 1 2 4 4 0 4 5 5
1 2 4
Solve it your way first
Community solutions unlock after you submit a passing solution. Don't peek.
Ready to solve this challenge?
Create a free account to write code, submit solutions, and track your progress.