You are given a tree with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Consider the following pseudocode, which performs a preorder traversal of the tree:
a = []
function preorder_traversal(u):
append u to the back of a
for each v that is adjacent to u and not visited:
preorder_traversal(v)
Note that the resulting array $$$a$$$, representing the preorder traversal of the tree starting from node $$$u$$$, may vary depending on the order in which adjacent vertices are visited.
Let $$$S_u$$$ denote the set of all possible arrays $$$a$$$ that can be obtained by running the above traversal starting from vertex $$$u$$$, allowing all possible orderings of adjacent nodes during traversal.
For any two vertices $$$u$$$ and $$$v$$$, define the function $$$f(u, v)$$$ as follows: $$$$$$\min_{a_1 \in S_u, a_2 \in S_v} \text{LCS}(a_1, a_2)$$$$$$ where $$$\text{LCS}(a_1, a_2)$$$ denotes the length of the longest common subsequence between the two arrays $$$a_1$$$ and $$$a_2$$$.
For example, consider a tree with $$$5$$$ vertices and edges $$$(1,2),(1,3), (3,4), (3,5)$$$. Then we have:
Then we have $$$f(1,4) = 2$$$, because the smallest possible LCS length among all combinations of arrays from $$$S_1$$$ and $$$S_4$$$ is 2.
You are given $$$q$$$ queries. Each query consists of two (not necessarily distinct) vertices $$$x_i$$$ and $$$y_i$$$. For each query, compute the value of $$$f(x_i, y_i)$$$.
The first line of each test case contains two integers $$$n$$$ and $$$q$$$ — the number of vertices of the tree and the number of queries.
The $$$i$$$-th of the following $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ — the ends of the $$$i$$$-th edge.
The $$$i$$$-th of the following $$$q$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ — the vertices in the query.
For each query, output a single integer in a new line — the value of $$$f(x_i,y_i)$$$.
5 2 1 2 1 3 3 4 3 5 1 4 3 3
2 3
7 2 1 2 2 3 2 5 1 4 4 6 4 7 5 6 3 5
2 4
| Name |
|---|


