题库练习 Tree Queries
← 上一题 下一题 →

A14756 | Tree Queries

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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.

输入输出样例

输入 #1
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
输出 #1
5
2
1
4
0
4
C++ 编辑器
输入
输出