AT_abc460_f.Farthest Pair Query

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

There is a tree with NN vertices. The vertices are numbered 1,2,…,N1, 2, \ldots, N, and the ii-th edge connects vertices UiU_i and ViV_i.

Initially, all vertices are painted black.

Process QQ queries of the following form in order and find the answer for each query.

  • An integer xx (1≤x≤N)(1 \leq x \leq N) is given. If vertex xx is white, repaint it black; if vertex xx is black, repaint it white. Then, find the maximum distance between two black vertices. Here, the distance between two vertices on a tree is the number of edges in the simple path between them.

In the given inputs, there are always at least two black vertices when processing the queries in order.

有一棵包含 NN 个顶点的树。顶点编号为 1,2,…,N1, 2, \ldots, N,其中第 ii 条边连接顶点 UiU_i 和 ViV_i。

初始时,所有顶点均为黑色。

按顺序处理 QQ 个如下形式的查询,并对每个查询输出答案:

  • 给定一个整数 xx(1≤x≤N1 \leq x \leq N)。若顶点 xx 为白色,则将其涂成黑色;若为黑色,则将其涂成白色。然后,求所有黑色顶点之间两两距离的最大值。此处,树上两个顶点之间的距离定义为它们之间唯一简单路径所含的边数。

在给定的输入中,按顺序处理查询时,黑色顶点的数量始终不少于两个。

输入格式

The input is given from Standard Input in the following format, where queryi\text{query}_i denotes the ii-th query:

NN
U1U_1 V1V_1
U2U_2 V2V_2
⋮\vdots
UN−1U_{N-1} VN−1V_{N-1}
QQ
query1\text{query}_1
query2\text{query}_2
⋮\vdots
queryQ\text{query}_Q

Each query is given in the following format:

xx

输入从标准输入中以如下格式给出,其中 queryi\text{query}_i 表示第 ii 个查询:

NN
U1U_1 V1V_1
U2U_2 V2V_2
⋮\vdots
UN−1U_{N-1} VN−1V_{N-1}
QQ
query1\text{query}_1
query2\text{query}_2
⋮\vdots
queryQ\text{query}_Q

每个查询的格式如下:

xx

输出格式

Output QQ lines.

The ii-th line should contain the answer for the ii-th query when processed in order.

输出 QQ 行。

第 ii 行应包含按顺序处理的第 ii 个查询的答案。

输入输出样例

  • 输入#1

    7
    1 2
    2 3
    2 4
    6 7
    3 5
    7 3
    9
    1
    4
    2
    6
    3
    1
    1
    4
    6

    输出#1

    4
    3
    3
    2
    2
    3
    2
    3
    4

说明/提示

Sample 1 Explanation:

  • After the repainting in the 11st query, the black vertices are 2,3,4,5,6,72, 3, 4, 5, 6, 7. The distance between vertices 44 and 66 is 44, and the answer is 44.
  • After the repainting in the 22nd query, the black vertices are 2,3,5,6,72, 3, 5, 6, 7. The distance between vertices 22 and 66 is 33, and the answer is 33.
  • After the repainting in the 33rd query, the black vertices are 3,5,6,73, 5, 6, 7. The distance between vertices 55 and 66 is 33, and the answer is 33.
  • After the repainting in the 44th query, the black vertices are 3,5,73, 5, 7. The distance between vertices 55 and 77 is 22, and the answer is 22.
  • After the repainting in the 55th query, the black vertices are 5,75, 7. The distance between vertices 55 and 77 is 22, and the answer is 22.
  • After the repainting in the 66th query, the black vertices are 1,5,71, 5, 7. The distance between vertices 11 and 55 is 33, and the answer is 33.
  • After the repainting in the 77th query, the black vertices are 5,75, 7. The distance between vertices 55 and 77 is 22, and the answer is 22.
  • After the repainting in the 88th query, the black vertices are 4,5,74, 5, 7. The distance between vertices 44 and 55 is 33, and the answer is 33.
  • After the repainting in the 99th query, the black vertices are 4,5,6,74, 5, 6, 7. The distance between vertices 44 and 66 is 44, and the answer is 44.

Note that the examples given above are just one instance achieving the maximum value.

Constraints

  • 3≤N≤1053 \leq N \leq 10^5
  • 1≤Ui,Vi≤N1 \leq U_i, V_i \leq N
  • The given graph is a tree.
  • 1≤Q≤1051 \leq Q \leq 10^5
  • For each query, 1≤x≤N1 \leq x \leq N.
  • There are always at least two black vertices.
  • All input values are integers.

样例 1 解释:

  • 第 11 次查询重绘后,黑色顶点为 2,3,4,5,6,72, 3, 4, 5, 6, 7。顶点 44 与 66 之间的距离为 44,答案为 44。
  • 第 22 次查询重绘后,黑色顶点为 2,3,5,6,72, 3, 5, 6, 7。顶点 22 与 66 之间的距离为 33,答案为 33。
  • 第 33 次查询重绘后,黑色顶点为 3,5,6,73, 5, 6, 7。顶点 55 与 66 之间的距离为 33,答案为 33。
  • 第 44 次查询重绘后,黑色顶点为 3,5,73, 5, 7。顶点 55 与 77 之间的距离为 22,答案为 22。
  • 第 55 次查询重绘后,黑色顶点为 5,75, 7。顶点 55 与 77 之间的距离为 22,答案为 22。
  • 第 66 次查询重绘后,黑色顶点为 1,5,71, 5, 7。顶点 11 与 55 之间的距离为 33,答案为 33。
  • 第 77 次查询重绘后,黑色顶点为 5,75, 7。顶点 55 与 77 之间的距离为 22,答案为 22。
  • 第 88 次查询重绘后,黑色顶点为 4,5,74, 5, 7。顶点 44 与 55 之间的距离为 33,答案为 33。
  • 第 99 次查询重绘后,黑色顶点为 4,5,6,74, 5, 6, 7。顶点 44 与 66 之间的距离为 44,答案为 44。

注意:上述示例仅为达到最大值的一种可能情形。

约束条件

  • 3≤N≤1053 \leq N \leq 10^5
  • 1≤Ui,Vi≤N1 \leq U_i, V_i \leq N
  • 给定图是一棵树。
  • 1≤Q≤1051 \leq Q \leq 10^5
  • 对于每次查询,1≤x≤N1 \leq x \leq N。
  • 黑色顶点数量始终不少于两个。
  • 所有输入值均为整数。

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

首页