Nodes at given depth MEDIUM Arbori (BAC) 25 XP 0 solved
Mobile coding works. A laptop is faster for long sessions.

Problem

A tree with `n` nodes numbered from `1` to `n` is represented by the parent array `t`, where `t[i]` is the parent of node `i`, and `t[i] = 0` means that node `i` is the root of the tree. The root is at depth `0`, children of the root are at depth `1`, and in general a node is at depth equal to the number of edges on the path from it to the root. An additional natural number `d` is read. Print, in ascending order of index, all nodes that are at depth `d`. If no such node exists, print `0`.

Input format

Input input.txt

The first line contains two natural numbers `n` and `d`, separated by a space. The second line contains the `n` values of the parent array, separated by spaces. - `1 <= n <= 100` - `0 <= d <= n` - the array describes a valid tree

Output format

Output output.txt

The program prints the indices of the nodes at depth `d`, in ascending order, separated by spaces, or `0` if no such node exists.

Example

input
7 2

2 4 4 0 4 5 5
output
1 6 7

Ready to solve this challenge?

Create a free account to write code, submit solutions, and track your progress.

⌨️ Keyboard Shortcuts

Code Editor
Run Code
Ctrl Enter
Submit Code
Ctrl Shift Enter
Format Code
Shift Alt F
Toggle Comment
Ctrl /
Undo
Ctrl Z
Redo
Ctrl Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc

🔍 Interactive Debugger

0 / 0

Analyzing your code...

📦 Variables

No variables yet

📚 Call Stack
main() line 1
📤 Output
We use cookies

Essential cookies are always active. You can choose to enable preference and analytics cookies. Learn more