Robot on tree - MarisaOJ: Marisa Online Judge

Robot on tree

Time limit: 1000 ms
Memory limit: 256 MB
You are given a tree of $n$ vertices. You have a robot. You can place it at a vertex $u$ on the tree and order it to move to $v$ using the simple path between 2 vertices. Moving from one vertex to another costs $1$ energy. When the robot runs out of energy, it stops. There are $q$ queries, each of the form $(u, v, w)$, the robot starts at $u$ and moves to $v$ with $w$ energy initially. Where will it stop? ### Input - The first line contain 2 integers $n, q$. - The next $n - 1$ lines, each line contains 2 integers $u, v$, there is an edge between $u$ and $v$. - The next $q$ lines, each line contains 3 integers $u, v, w$, an query. ### Output - Print $q$ integers, the answers to $q$ queries. ### Constraints - $1 \le n, q \le 10^5$. - $1 \le u, v, w \le n$. ### Example Input: ``` 7 3 1 2 1 3 2 4 2 5 3 6 3 7 1 4 1 5 6 2 6 5 3 ``` Output: ``` 2 1 2 ```