| Innopolis Open 2024. Final round |
|---|
| Finished |
In the data center of the University of Innopolis there are $$$n$$$ servers numbered with consecutive integers from $$$1$$$ to $$$n$$$. The servers are connected by $$$n - 1$$$ optical fiber cables in such a way that data transmission is possible between any two servers through one or multiple cables. In other words, the connections between servers form a tree.
Each optical fiber cable transmits data with a delay of $$$1$$$ unit of time, and the delay between two servers is equal to the distance in the tree between these servers.
It is required to select several servers for a distributed data storage system in such a way that the delay between any two servers in the system does not exceed $$$d$$$. Let the selected servers be $$$s_1, s_2, \ldots, s_k$$$, then the total delay of the system is the sum of delays between all $$$\frac{k (k - 1)}{2}$$$ pairs of servers $$$s_i$$$. Formally:
Your task is to answer $$$q$$$ queries. Each query is defined by a number of a server $$$u$$$ and represents the following question: if a specific server $$$u$$$ is included in the data storage system, what is the maximum possible total delay of the system?
The first line of the input contains two integers $$$n$$$ and $$$d$$$ — the number of servers in the data center and the maximum allowed delay between two servers ($$$2 \le n \le 5 \cdot 10^5$$$; $$$0 \le d \le n - 1$$$).
Each of the next $$$n - 1$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ — the numbers of two servers directly connected by the $$$i$$$-th optical cable ($$$1 \le a_i, b_i \le n$$$). It is guaranteed that it's possible to transmit data between any two servers through one or multiple consecutive cables.
The next line contains a single integer $$$q$$$ — the number of queries ($$$1 \le q \le 10$$$). In the $$$i$$$-th of the next $$$q$$$ lines, a single integer $$$u_i$$$ is given — the number of the server from the $$$i$$$-th query ($$$1 \le u_i \le n$$$).
For each query, output a single integer on a separate line — the maximum total delay of the system when the corresponding server is included in the data storage system.
Points for each subtask from 1 to 4 are awarded only if all tests for this subtask and the necessary subtasks are successfully passed.
Each test in subtasks 5 and 6 is scored independently and costs 3 points in subtask 5 or 2 points in subtask 6.
| Subtask | Points | Constraints | Necessary subtasks | Feedback policy |
| 0 | – | examples | full | |
| 1 | 12 | $$$n \le 15$$$ | 0 | first error |
| 2 | 11 | $$$d = n - 1$$$ | first error | |
| 3 | 13 | $$$n \le 300$$$ | 0, 1 | first error |
| 4 | 16 | $$$n \le 5000$$$ | 0, 1, 3 | first error |
| 5 | $$$8 \times 3$$$ | $$$n \le 10^5$$$ | 0, 1, 3, 4 | full |
| 6 | $$$12 \times 2$$$ | no additional constraints | 0 – 6 | full |
7 22 11 32 45 44 76 577654321
9 4 9 9 4 9 4
2 01 211
0
In the first example the responses to the queries correspond to the following sets of selected servers:
| Name |
|---|


