Preparando MOJI
You are given a tree consisting of $$$n$$$ vertices. Recall that a tree is an undirected connected acyclic graph. The given tree is rooted at the vertex $$$1$$$.
You have to process $$$q$$$ queries. In each query, you are given a vertex of the tree $$$v$$$ and an integer $$$k$$$.
To process a query, you may delete any vertices from the tree in any order, except for the root and the vertex $$$v$$$. When a vertex is deleted, its children become the children of its parent. You have to process a query in such a way that maximizes the value of $$$c(v) - m \cdot k$$$ (where $$$c(v)$$$ is the resulting number of children of the vertex $$$v$$$, and $$$m$$$ is the number of vertices you have deleted). Print the maximum possible value you can obtain.
The queries are independent: the changes you make to the tree while processing a query don't affect the tree in other queries.
The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of vertices in the tree.
Then $$$n-1$$$ lines follow, the $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$; $$$x_i \ne y_i$$$) — the endpoints of the $$$i$$$-th edge. These edges form a tree.
The next line contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries.
Then $$$q$$$ lines follow, the $$$j$$$-th of them contains two integers $$$v_j$$$ and $$$k_j$$$ ($$$1 \le v_j \le n$$$; $$$0 \le k_j \le 2 \cdot 10^5$$$) — the parameters of the $$$j$$$-th query.
For each query, print one integer — the maximum value of $$$c(v) - m \cdot k$$$ you can achieve.
8 6 7 3 2 8 3 5 7 7 4 7 1 7 3 6 1 0 1 2 1 3 7 1 5 0 7 200000
5 2 1 4 0 4
The tree in the first example is shown in the following picture:
Answers to the queries are obtained as follows: