n huts numbered 0 to n - 1, linked by the trails in trails: an entry [u, v] is a two-way trail between huts u and v. The trails form a tree, so there is exactly one path between any two huts.
A request requests[i] = [start, end, hut] means a group walks the path from start to end, and a friend in hut hut wants to join it. Find the hut on the group's path (including start and end) that is the fewest trail hops away from hut.
Return an array whose i-th element is that hut for the i-th request.n=6, trails=[[0, 1], [1, 2], [2, 3], [1, 4], [4, 5]], requests=[[0, 3, 5], [3, 2, 0], [5, 4, 5]] → [1, 2, 5]
The route is 0 → 1 → 2 → 3, and from hut 5 the nearest point is 1 (two hops via 4). The route 3 → 2: from 0 it is two hops to 2 and three to 3, so the meeting point is 2. In the third request hut 5 lies on the route itself.
n=2, trails=[[0, 1]], requests=[[1, 1, 0]] → [1]
The route is just point 1, so that is the answer.
n=6, trails=[[0, 1], [1, 2], [2, 3], [1, 4], [4, 5]], requests=[[0, 3, 5], [3, 2, 0], [5, 4, 5]]
[1, 2, 5]
Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.