You can find the problem here.

Statement

This is a two-pass problem.

In the first pass, you are given an undirected tree with 𝑛 nodes, and you may relabel these nodes however you like; the only constraint is that all labels must be distinct.

In the second pass, you must implement a procedure that takes the following inputs:

  • 𝑠, a source node
  • 𝐿, the labels of the nodes directly adjacent to 𝑠
  • 𝑡, a destination node And returns the unique label 𝑙 in 𝐿 such that 𝑙 lies on the path from 𝑠 to 𝑡. In words, you must be able to route 𝑠 to 𝑡 with only the knowledge of adjacent labels at each step.

In order to earn full points, we must only use labels 0𝑛1.

Solution

This essentially means that for an edge (𝑠,𝑙), using only the numbers 𝑠, 𝑙, and 𝐿, we must be able to determine which nodes lie on the other side of this edge (i.e. closer to 𝑙). A natural way to do so is by assigning each subtree to a range of values.

fragment

You can find my code here.