← All problemsSign in

MEX on Trees

CodeChefRating 2322Open on judge ↗

Consider a tree with $N$ nodes, rooted at node $1$. The value of the $i^{th}$ node $(1 \leq i \leq N)$ is denoted by $A_i$. Chef defines the function $MEX(u, v)$ as follows: Let $B$ denote the set of **all** the values of nodes that lie in the **shortest path** from $u$ to $v$ (including both $u$ and $v$). Then, $MEX(u, v)$ denotes the [MEX](https://en.wikipedia.org/wiki/Mex_(mathematics)) of

HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17

CodeSearch Tutor

Hints, not spoilers — it won’t hand over the full solution unless you insist.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start