Imagine you're exploring a magical tree where each node holds a special value, and traveling from the root to any node creates a unique XOR signature by combining all values along the path!
You are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1. Each node i has an integer value vals[i], and its parent is given by par[i].
The path XOR sum from the root to a node u is defined as the bitwise XOR of all vals[i] for nodes i on the path from the root node to node u, inclusive.
You are given a 2D integer array queries, where queries[j] = [u_j, k_j]. For each query, you need to:
- Find all nodes in the subtree rooted at u_j
- Calculate the path XOR sum for each of these nodes
- Find the k_j-th smallest distinct path XOR sum
- Return
-1if there are fewer thank_jdistinct values
Remember: In a rooted tree, the subtree of a node v includes v and all nodes whose path to the root passes through v.
Input & Output
Constraints
- 1 โค n โค 104
- 0 โค vals[i] โค 109
- par[0] = 0 (root points to itself)
- For i > 0: 0 โค par[i] < i
- 1 โค queries.length โค 104
- 0 โค uj < n
- 1 โค kj โค n
- The parent array represents a valid tree structure