A company org chart forms a rooted tree with n people (nodes), labeled 1..n.
You are given an array parent[1..n] describing the tree:
parent[i] = -1
means
i
is the root (top leader)
parent[i]
is the direct manager (parent) of person
i
When a person u issues a command, it is sent to all subordinates of u (all descendants of u), using this strict order:
u
’s direct children, sorted by increasing person id.
This defines a deterministic traversal order over the descendants of u:
u
’s subtree
excluding u
, where children are visited in ascending id.
Given:
parent[1..n]
u
k
(1-indexed)
Return the person id of the k-th subordinate who receives the command in the propagation order.
If u has fewer than k descendants, return -1.
parent[]
,
u
,
k
-1
k
is 1-indexed over recipients (descendants only; the issuer
u
is not counted).
n
up to 2e5), so an approach better than simulating propagation step-by-step for each query is typically expected (if multiple queries are present, state how many).