AT_abc460_f.Farthest Pair Query
提高+/省选-
通过率:0%
时间限制:4.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a tree with N vertices. The vertices are numbered 1,2,…,N, and the i-th edge connects vertices Ui and Vi.
Initially, all vertices are painted black.
Process Q queries of the following form in order and find the answer for each query.
- An integer x (1≤x≤N) is given. If vertex x is white, repaint it black; if vertex x 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.
有一棵包含 N 个顶点的树。顶点编号为 1,2,…,N,其中第 i 条边连接顶点 Ui 和 Vi。
初始时,所有顶点均为黑色。
按顺序处理 Q 个如下形式的查询,并对每个查询输出答案:
- 给定一个整数 x(1≤x≤N)。若顶点 x 为白色,则将其涂成黑色;若为黑色,则将其涂成白色。然后,求所有黑色顶点之间两两距离的最大值。此处,树上两个顶点之间的距离定义为它们之间唯一简单路径所含的边数。
在给定的输入中,按顺序处理查询时,黑色顶点的数量始终不少于两个。
输入格式
The input is given from Standard Input in the following format, where queryi denotes the i-th query:
N
U1 V1
U2 V2
⋮
UN−1 VN−1
Q
query1
query2
⋮
queryQ
Each query is given in the following format:
x
输入从标准输入中以如下格式给出,其中 queryi 表示第 i 个查询:
N
U1 V1
U2 V2
⋮
UN−1 VN−1
Q
query1
query2
⋮
queryQ
每个查询的格式如下:
x
输出格式
Output Q lines.
The i-th line should contain the answer for the i-th query when processed in order.
输出 Q 行。
第 i 行应包含按顺序处理的第 i 个查询的答案。
输入输出样例
输入#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 1st query, the black vertices are 2,3,4,5,6,7. The distance between vertices 4 and 6 is 4, and the answer is 4.
- After the repainting in the 2nd query, the black vertices are 2,3,5,6,7. The distance between vertices 2 and 6 is 3, and the answer is 3.
- After the repainting in the 3rd query, the black vertices are 3,5,6,7. The distance between vertices 5 and 6 is 3, and the answer is 3.
- After the repainting in the 4th query, the black vertices are 3,5,7. The distance between vertices 5 and 7 is 2, and the answer is 2.
- After the repainting in the 5th query, the black vertices are 5,7. The distance between vertices 5 and 7 is 2, and the answer is 2.
- After the repainting in the 6th query, the black vertices are 1,5,7. The distance between vertices 1 and 5 is 3, and the answer is 3.
- After the repainting in the 7th query, the black vertices are 5,7. The distance between vertices 5 and 7 is 2, and the answer is 2.
- After the repainting in the 8th query, the black vertices are 4,5,7. The distance between vertices 4 and 5 is 3, and the answer is 3.
- After the repainting in the 9th query, the black vertices are 4,5,6,7. The distance between vertices 4 and 6 is 4, and the answer is 4.
Note that the examples given above are just one instance achieving the maximum value.
Constraints
- 3≤N≤105
- 1≤Ui,Vi≤N
- The given graph is a tree.
- 1≤Q≤105
- For each query, 1≤x≤N.
- There are always at least two black vertices.
- All input values are integers.
样例 1 解释:
- 第 1 次查询重绘后,黑色顶点为 2,3,4,5,6,7。顶点 4 与 6 之间的距离为 4,答案为 4。
- 第 2 次查询重绘后,黑色顶点为 2,3,5,6,7。顶点 2 与 6 之间的距离为 3,答案为 3。
- 第 3 次查询重绘后,黑色顶点为 3,5,6,7。顶点 5 与 6 之间的距离为 3,答案为 3。
- 第 4 次查询重绘后,黑色顶点为 3,5,7。顶点 5 与 7 之间的距离为 2,答案为 2。
- 第 5 次查询重绘后,黑色顶点为 5,7。顶点 5 与 7 之间的距离为 2,答案为 2。
- 第 6 次查询重绘后,黑色顶点为 1,5,7。顶点 1 与 5 之间的距离为 3,答案为 3。
- 第 7 次查询重绘后,黑色顶点为 5,7。顶点 5 与 7 之间的距离为 2,答案为 2。
- 第 8 次查询重绘后,黑色顶点为 4,5,7。顶点 4 与 5 之间的距离为 3,答案为 3。
- 第 9 次查询重绘后,黑色顶点为 4,5,6,7。顶点 4 与 6 之间的距离为 4,答案为 4。
注意:上述示例仅为达到最大值的一种可能情形。
约束条件
- 3≤N≤105
- 1≤Ui,Vi≤N
- 给定图是一棵树。
- 1≤Q≤105
- 对于每次查询,1≤x≤N。
- 黑色顶点数量始终不少于两个。
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?