CF1606F.Tree Queries

省选/NOI-

通过率:0%

时间限制:6.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree consisting of nn vertices. Recall that a tree is an undirected connected acyclic graph. The given tree is rooted at the vertex 11.

You have to process qq queries. In each query, you are given a vertex of the tree vv and an integer kk.

To process a query, you may delete any vertices from the tree in any order, except for the root and the vertex vv. 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⋅kc(v) - m \cdot k (where c(v)c(v) is the resulting number of children of the vertex vv, and mm 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.

给你一棵包含 nn 个顶点的树。注意:树是一种无向、连通且无环的图。给定的树以顶点 11 为根。

你需要处理 qq 个查询。在每个查询中,你将获得树中的一个顶点 vv 和一个整数 kk。

为了处理一个查询,你可以按任意顺序删除树中的任意顶点,但根节点(即顶点 11)和顶点 vv 不得被删除。当一个顶点被删除时,它的所有子节点将直接成为其父节点的子节点。你需要以某种方式处理该查询,使得表达式 c(v)−m⋅kc(v) - m \cdot k 的值最大化(其中 c(v)c(v) 表示操作后顶点 vv 的子节点数量,mm 表示你所删除的顶点总数)。请输出所能得到的最大可能值。

各个查询相互独立:你在处理某个查询时对树所作的修改,不会影响其他查询中树的初始状态。

输入格式

The first line contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of vertices in the tree.

Then n−1n-1 lines follow, the ii-th of them contains two integers xix_i and yiy_i (1≤xi,yi≤n1 \le x_i, y_i \le n; xi≠yix_i \ne y_i) — the endpoints of the ii-th edge. These edges form a tree.

The next line contains one integer qq (1≤q≤2⋅1051 \le q \le 2 \cdot 10^5) — the number of queries.

Then qq lines follow, the jj-th of them contains two integers vjv_j and kjk_j (1≤vj≤n1 \le v_j \le n; 0≤kj≤2⋅1050 \le k_j \le 2 \cdot 10^5) — the parameters of the jj-th query.

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)——树中顶点的数量。

接下来 n−1n-1 行,其中第 ii 行包含两个整数 xix_i 和 yiy_i(1≤xi,yi≤n1 \le x_i, y_i \le n;xi≠yix_i \ne y_i)——第 ii 条边的两个端点。这些边构成一棵树。

接下来一行包含一个整数 qq(1≤q≤2⋅1051 \le q \le 2 \cdot 10^5)——查询的数量。

然后是 qq 行,其中第 jj 行包含两个整数 vjv_j 和 kjk_j(1≤vj≤n1 \le v_j \le n;0≤kj≤2⋅1050 \le k_j \le 2 \cdot 10^5)——第 jj 个查询的参数。

输出格式

For each query, print one integer — the maximum value of c(v)−m⋅kc(v) - m \cdot k you can achieve.

对于每个查询,输出一个整数——你能达到的 c(v)−m⋅kc(v) - m \cdot k 的最大值。

输入输出样例

  • 输入#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

说明/提示

The tree in the first example is shown in the following picture:

Answers to the queries are obtained as follows:

  1. v=1,k=0v=1,k=0: you can delete vertices 77 and 33, so the vertex 11 has 55 children (vertices 22, 44, 55, 66, and 88), and the score is 5−2⋅0=55 - 2 \cdot 0 = 5;
  2. v=1,k=2v=1,k=2: you can delete the vertex 77, so the vertex 11 has 44 children (vertices 33, 44, 55, and 66), and the score is 4−1⋅2=24 - 1 \cdot 2 = 2.
  3. v=1,k=3v=1,k=3: you shouldn't delete any vertices, so the vertex 11 has only one child (vertex 77), and the score is 1−0⋅3=11 - 0 \cdot 3 = 1;
  4. v=7,k=1v=7,k=1: you can delete the vertex 33, so the vertex 77 has 55 children (vertices 22, 44, 55, 66, and 88), and the score is 5−1⋅1=45 - 1 \cdot 1 = 4;
  5. v=5,k=0v=5,k=0: no matter what you do, the vertex 55 will have no children, so the score is 00;
  6. v=7,k=200000v=7,k=200000: you shouldn't delete any vertices, so the vertex 77 has 44 children (vertices 33, 44, 55, and 66), and the score is 4−0⋅200000=44 - 0 \cdot 200000 = 4.

第一个示例中的树如下图所示:

各查询的答案计算方式如下:

  1. v=1,k=0v=1,k=0:可删除顶点 77 和 33,此时顶点 11 拥有 55 个子节点(顶点 22、44、55、66 和 88),得分为 5−2⋅0=55 - 2 \cdot 0 = 5;
  2. v=1,k=2v=1,k=2:可删除顶点 77,此时顶点 11 拥有 44 个子节点(顶点 33、44、55 和 66),得分为 4−1⋅2=24 - 1 \cdot 2 = 2;
  3. v=1,k=3v=1,k=3:不应删除任何顶点,此时顶点 11 仅有一个子节点(顶点 77),得分为 1−0⋅3=11 - 0 \cdot 3 = 1;
  4. v=7,k=1v=7,k=1:可删除顶点 33,此时顶点 77 拥有 55 个子节点(顶点 22、44、55、66 和 88),得分为 5−1⋅1=45 - 1 \cdot 1 = 4;
  5. v=5,k=0v=5,k=0:无论采取何种操作,顶点 55 均无子节点,因此得分为 00;
  6. v=7,k=200000v=7,k=200000:不应删除任何顶点,此时顶点 77 拥有 44 个子节点(顶点 33、44、55 和 66),得分为 4−0⋅200000=44 - 0 \cdot 200000 = 4。

输入解题思路,AI测评打分。不知道怎么写?

首页