bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n.
Let f(a,b) be the minimum of vertices
not on the path between vertices a and b.
There are q queries (u
i,v
i) for the value of f(u
i,v
i). Help bobo answer them.
The input consists of several tests. For each tests:
The first line contains 2 integers n,q (4≤n≤10
6,1≤q≤10
6). Each of the following (n - 1) lines contain 2 integers a
i,b
i denoting an edge between vertices a
i and b
i (1≤a
i,b
i≤n). Each of the following q lines contains 2 integer u′
i,v′
i (1≤u
i,v
i≤n).
The queries are encrypted in the following manner.
u
1=u′
1,v
1=v′
1.
For i≥2, u
i=u′
i⊕f(u
i - 1,v
i - 1),v
i=v′
i⊕f(u
i-1,v
i-1).
Note ⊕ denotes bitwise exclusive-or.
It is guaranteed that f(a,b) is defined for all a,b.
The task contains huge inputs. `scanf` in g++ is considered too slow to get accepted. You may (1) submit the solution in c++; or (2) use hand-written input utilities.
For each tests:
For each queries, a single number denotes the value.