| UDESC Selection Contest 2024-1 |
|---|
| Finished |
Joãozinho is an astronomer who has just started his master's degree at UFMG, studying the "worms" of the multiverse.
The multiverse is the set of all universes that exist, and Joãozinho is researching a hierarchical way to represent it. In his studies, each universe in the multiverse is assigned an integer $$$u$$$ as its identifier, and in each universe, there are several giant worms that allow travel to other universes.
The first thing Joãozinho did was to write down all the facts he knows about the multiverse. Here is everything he recorded:
Joãozinho's master's advisor, Karina, gave him a task to simulate any multiverse that obeys the rules of the real multiverse, and this simulation must be able to answer very specific questions about any multiverse.
After weeks of hard work, Joãozinho is almost done, but one "sub-task" remains, and he can't solve it. It is defined as follows:
Where $$$f(i, j)$$$ is the number representing the universe with the smallest number of stars from which it is possible to travel to universes $$$a_i, a_{i + 1}, \cdots, a_{j - 1}, a_j$$$.
Joãozinho is out of ideas on how to solve this, so he asked for your help to write a program that receives $$$Q$$$ such queries and answers them.
The first line of input contains two integers $$$N$$$ $$$(1 \le N \le 10^5)$$$ and $$$Q$$$ $$$(1 \le Q \le 10^5)$$$, the number of universes in the simulated multiverse and the number of queries, respectively.
The next $$$N - 1$$$ lines each contain two integers $$$u$$$ and $$$v$$$ $$$(1 \le u, v \le N)$$$, indicating that there is a giant worm in universe $$$u$$$ that allows travel to universe $$$v$$$.
It is guaranteed that the given multiverse satisfies all the facts and conventions Joãozinho recorded.
The next $$$Q$$$ lines each contain an integer $$$K$$$ $$$(1 \le K \le N)$$$, followed by $$$K$$$ integers $$$a_1, \cdots , a_K$$$, the universes in the query. It is guaranteed that all universes in a query are distinct, and also the sum of $$$K$$$ over all queries does not exceed $$$3 \cdot 10^5$$$.
The output must consist of $$$Q$$$ lines, each containing a single integer — the answer to the corresponding query.
5 21 21 33 43 51 22 4 5
2 12
4 11 21 31 43 2 3 4
12
1 11 1
1
Explanation for the first example:
In the first query, only one universe is given, so the universe with the fewest stars that can reach universe $$$2$$$ is universe $$$2$$$ itself. In the second query, the answer is $$$f(1, 1) + f(1, 2) + f(2, 2) = 4 + 3 + 5 = 12$$$.
| Name |
|---|


