Use a DFS Euler tour to represent every subtree of a vertex as an interval .
Let be Woohyeon's current vertex, and maintain for every vertex . If Woohyeon moves from to an adjacent vertex along an edge of length , distances to vertices on the side of that edge decrease by , while distances to vertices on the side increase by .
If is a child of , add to every distance, then subtract from the subtree of . If is the parent of , subtract from every distance, then add to the subtree of .
Build a lazy segment tree whose nodes store two minima: the minimum distance to a selected vertex and the minimum distance to a not-selected vertex. A range addition shifts both minima. A range state flip swaps the two minima. Both updates can be performed by lazy propagation. For a type- query, read the minimum for selected vertices at the segment-tree root, or return if none exists.
Preprocessing takes time. Every query takes time, with memory. Use 64-bit integers for distances and updates.