CF1783G.Weighed Tree Radius

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given a tree of nn vertices and n1n - 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 auava_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)=max1unwv(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=min1vne(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.

输入格式

The first line contains the single integer nn ( 2n21052 \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 ( 0ai1060 \le a_i \le 10^6 ) — the initial weights of vertices.

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

The next line contains the single integer mm ( 1m1051 \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 ( 1vjn1 \le v_j \le n ; 0xj1060 \le x_j \le 10^6 ) — a vertex and it's new weight.

输出格式

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

输入输出样例

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

首页