CF1783G.Weighed Tree Radius

省选/NOI-

通过率:0%

时间限制:6.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given a tree of nn vertices and n−1n - 1 edges. The ii-th vertex has an initial weight aia_i.

Let the distance dv(u)d_v(u) from vertex vv to vertex uu be the number of edges on the path from vv to uu. Note that dv(u)=du(v)d_v(u) = d_u(v) and dv(v)=0d_v(v) = 0.

Let the weighted distance wv(u)w_v(u) from vv to uu be wv(u)=dv(u)+auw_v(u) = d_v(u) + a_u. Note that wv(v)=avw_v(v) = a_v and wv(u)≠wu(v)w_v(u) \neq w_u(v) if au≠ava_u \neq a_v.

Analogically to usual distance, let's define the eccentricity e(v)e(v) of vertex vv as the greatest weighted distance from vv to any other vertex (including vv itself), or e(v)=max⁡1≤u≤nwv(u)e(v) = \max\limits_{1 \le u \le n}{w_v(u)}.

Finally, let's define the radius rr of the tree as the minimum eccentricity of any vertex, or r=min⁡1≤v≤ne(v)r = \min\limits_{1 \le v \le n}{e(v)}.

You need to perform mm queries of the following form:

  • vjv_j xjx_j — assign avj=xja_{v_j} = x_j.

After performing each query, print the radius rr of the current tree.

给你一棵包含 nn 个顶点和 n−1n - 1 条边的树。第 ii 个顶点的初始权重为 aia_i。

定义顶点 vv 到顶点 uu 的距离 dv(u)d_v(u) 为从 vv 到 uu 的路径上的边数。注意:dv(u)=du(v)d_v(u) = d_u(v),且 dv(v)=0d_v(v) = 0。

定义顶点 vv 到顶点 uu 的加权距离 wv(u)w_v(u) 为 wv(u)=dv(u)+auw_v(u) = d_v(u) + a_u。注意:wv(v)=avw_v(v) = a_v,且当 au≠ava_u \neq a_v 时,有 wv(u)≠wu(v)w_v(u) \neq w_u(v)。

类比于通常的距离概念,定义顶点 vv 的离心率 e(v)e(v) 为 vv 到任意其他顶点(包括 vv 自身)的最大加权距离,即 e(v)=max⁡1≤u≤nwv(u)e(v) = \max\limits_{1 \le u \le n}{w_v(u)}。

最后,定义该树的半径 rr 为所有顶点离心率的最小值,即 r=min⁡1≤v≤ne(v)r = \min\limits_{1 \le v \le n}{e(v)}。

你需要执行 mm 次如下形式的查询:

  • vjv_j xjx_j — 将 avja_{v_j} 赋值为 xjx_j。

每次查询执行完毕后,请输出当前树的半径 rr。

输入格式

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

The second line contains nn integers a1,…,ana_1, \dots, a_n (0≤ai≤1060 \le a_i \le 10^6) — the initial weights of vertices.

Next n−1n - 1 lines contain edges of tree. The ii-th line contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n; ui≠viu_i \neq v_i) — the corresponding edge. The given edges form a tree.

The next line contains the single integer mm (1≤m≤1051 \le m \le 10^5) — the number of queries.

Next mm lines contain queries — one query per line. The jj-th query contains two integers vjv_j and xjx_j (1≤vj≤n1 \le v_j \le n; 0≤xj≤1060 \le x_j \le 10^6) — a vertex and it's new weight.

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

第二行包含 nn 个整数 a1,…,ana_1, \dots, a_n(0≤ai≤1060 \le a_i \le 10^6)——各顶点的初始权值。

接下来的 n−1n - 1 行描述树的边。第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n;ui≠viu_i \neq v_i)——对应的一条边。所给边构成一棵树。

下一行包含一个整数 mm(1≤m≤1051 \le m \le 10^5)——查询的数量。

接下来的 mm 行每行描述一个查询。第 jj 个查询包含两个整数 vjv_j 和 xjx_j(1≤vj≤n1 \le v_j \le n;0≤xj≤1060 \le x_j \le 10^6)——一个顶点及其新的权值。

输出格式

Print mm integers — the radius rr of the tree after performing each query.

输出 mm 个整数——每次查询操作后树的半径 rr。

输入输出样例

  • 输入#1

    6
    1 3 3 7 0 1
    2 1
    1 3
    1 4
    5 4
    4 6
    5
    4 7
    4 0
    2 5
    5 10
    5 5

    输出#1

    7
    4
    5
    10
    7

说明/提示

After the first query, you have the following tree:

The marked vertex in the picture is the vertex with minimum e(v)e(v), or r=e(4)=7r = e(4) = 7. The eccentricities of the other vertices are the following: e(1)=8e(1) = 8, e(2)=9e(2) = 9, e(3)=9e(3) = 9, e(5)=8e(5) = 8, e(6)=8e(6) = 8.

The tree after the second query:

The radius r=e(1)=4r = e(1) = 4.

After the third query, the radius r=e(2)=5r = e(2) = 5:

第一次查询后,你得到如下树:

图中标记的顶点是使 e(v)e(v) 取得最小值的顶点,即 r=e(4)=7r = e(4) = 7。其余各顶点的离心率如下:e(1)=8e(1) = 8,e(2)=9e(2) = 9,e(3)=9e(3) = 9,e(5)=8e(5) = 8,e(6)=8e(6) = 8。

第二次查询后的树:

此时半径为 r=e(1)=4r = e(1) = 4。

第三次查询后,半径为 r=e(2)=5r = e(2) = 5:

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

首页