#P8025. [ONTAK2015] Związek Harcerstwa Bajtockiego

[ONTAK2015] Związek Harcerstwa Bajtockiego

Problem Description

Given an unrooted tree with nn nodes, the distance between adjacent nodes is 11. At the beginning, you are at node mm. Then you will receive kk commands one by one. Each command contains two integers dd and tt. You need to move toward node dd along the shortest path within tt steps (including exactly tt steps). If you cannot reach dd within tt steps, then you stop at the last node you can reach. After each command, output your current position.

Input Format

The first line contains three integers n,m,kn, m, k.

The next n1n - 1 lines each contain two integers x,yx, y, representing an edge of the tree.

The next kk lines each contain two integers d,td, t, representing a command.

Output Format

One line containing kk integers, where the ii-th integer is the node you are at after executing the ii-th command.

3 1 2
1 2
2 3
3 4
1 1
3 2

Hint

For 100%100\% of the data, 1mn1061 \leq m \leq n \leq 10^6, 1k1061 \leq k \leq 10^6, 1x,y,dn1 \leq x, y, d \leq n, 0t1090 \leq t \leq 10^9.

Translated by ChatGPT 5