CF1192B.Dynamic Diameter

普及/提高-

通过率:0%

时间限制:6.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given a weighted undirected tree on nn vertices and a list of qq updates. Each update changes the weight of one edge. The task is to output the diameter of the tree after each update.

(The distance between two vertices is the sum of the weights on the unique simple path that connects them. The diameter is the largest of all those distances.)

给你一棵包含 nn 个顶点的带权无向树,以及一个包含 qq 次更新的操作列表。每次更新会修改某条边的权重。你的任务是在每次更新后输出该树的直径。

(两个顶点之间的距离定义为连接它们的唯一简单路径上所有边的权重之和;树的直径即为所有顶点对之间距离的最大值。)

输入格式

The first line contains three space-separated integers nn, qq and ww (2≤n≤100,000,1≤q≤100,0002 \leq n \leq 100,000, 1 \leq q \leq 100,000, 1≤w≤20,000,000,000,0001 \leq w \leq 20,000,000,000,000) – the number of vertices in the tree, the number of updates and the limit on the weights of edges. The vertices are numbered 11 through nn.

Next, n−1n-1 lines describing the initial tree follow. The ii-th of these lines contains three space-separated integers aia_i, bib_i, cic_i (1≤ai,bi≤n1 \leq a_i, b_i \leq n, 0≤ci<w0 \leq c_i \lt w) meaning that initially, there is an edge between vertices aia_i and bib_i with weight cic_i. It is guaranteed that these n−1n-1 lines describe a tree.

Finally, qq lines describing queries follow. The jj-th of these lines contains two space-separated integers djd_j, eje_j (0≤dj<n−1,0≤ej<w0 \leq d_j \lt n - 1, 0 \leq e_j \lt w). These two integers are then transformed according to the following scheme:

  • dj′=(dj+last) mod (n−1)d'_j = (d_j + last) \bmod (n - 1)
  • ej′=(ej+last) mod we'_j = (e_j + last) \bmod w

where lastlast is the result of the last query (initially last=0last=0). Tuple (dj′,ej′)(d'_j, e'_j) represents a query which takes the dj′+1d'_j+1-th edge from the input and sets its weight to ej′e'_j.

第一行包含三个以空格分隔的整数 nn、qq 和 ww(2≤n≤100,0002 \leq n \leq 100{,}000,1≤q≤100,0001 \leq q \leq 100{,}000,1≤w≤20,000,000,000,0001 \leq w \leq 20{,}000{,}000{,}000{,}000),分别表示树中顶点的数量、更新操作的次数以及边权的上限。顶点编号为 11 到 nn。

接下来是 n−1n-1 行,描述初始树的结构。其中第 ii 行包含三个以空格分隔的整数 aia_i、bib_i、cic_i(1≤ai,bi≤n1 \leq a_i, b_i \leq n,0≤ci<w0 \leq c_i \lt w),表示初始时顶点 aia_i 与 bib_i 之间存在一条权重为 cic_i 的边。保证这 n−1n-1 行所描述的是一棵树。

最后是 qq 行,每行描述一个查询。其中第 jj 行包含两个以空格分隔的整数 djd_j、eje_j(0≤dj<n−10 \leq d_j \lt n - 1,0≤ej<w0 \leq e_j \lt w)。这两个整数将按如下规则进行变换:

  • dj′=(dj+last) mod (n−1)d'_j = (d_j + last) \bmod (n - 1)
  • ej′=(ej+last) mod we'_j = (e_j + last) \bmod w

其中 lastlast 是上一次查询的结果(初始时 last=0last = 0)。元组 (dj′,ej′)(d'_j, e'_j) 表示一个查询:将输入中第 dj′+1d'_j+1 条边的权重修改为 ej′e'_j。

输出格式

Output qq lines. For each ii, line ii should contain the diameter of the tree after the ii-th update.

输出 qq 行。对于每个 ii,第 ii 行应包含第 ii 次更新后的树的直径。

输入输出样例

  • 输入#1

    4 3 2000
    1 2 100
    2 3 1000
    2 4 1000
    2 1030
    1 1020
    1 890

    输出#1

    2030
    2080
    2050
  • 输入#2

    10 10 10000
    1 9 1241
    5 6 1630
    10 5 1630
    2 6 853
    10 1 511
    5 3 760
    8 3 1076
    4 10 1483
    7 10 40
    8 2051
    5 6294
    5 4168
    7 1861
    0 5244
    6 5156
    3 3001
    8 5267
    5 3102
    8 3623

    输出#2

    6164
    7812
    8385
    6737
    6738
    7205
    6641
    7062
    6581
    5155

说明/提示

The first sample is depicted in the figure below. The left-most picture shows the initial state of the graph. Each following picture depicts the situation after an update. The weight of the updated edge is painted green, and the diameter is red.

The first query changes the weight of the 33rd edge, i.e. 2,4{2, 4}, to 10301030. The largest distance between any pair of vertices is 20302030 – the distance between 33 and 44.

As the answer is 20302030, the second query is $$d'_2 = (1 + 2030) \bmod 3 = 0$$ $$e'_2 = (1020 + 2030) \bmod 2000 = 1050$$ Hence the weight of the edge 1,2{1, 2} is changed to 10501050. This causes the pair 1,4{1, 4} to be the pair with the greatest distance, namely 20802080.

The third query is decoded as $$d'_3 = (1 + 2080) \bmod 3 = 2$$ $$e'_3 = (890 + 2080) \bmod 2000 = 970$$ As the weight of the edge 2,4{2, 4} decreases to 970970, the most distant pair is suddenly 1,3{1, 3} with 20502050.

第一个样例如下图所示。最左侧的图片展示了图的初始状态,随后每张图片展示一次更新后的状态。被更新的边的权重以绿色标出,图的直径以红色标出。

第一个查询将第 33 条边(即边 {2,4}\{2, 4\})的权重修改为 10301030。此时任意两个顶点之间的最大距离为 20302030——即顶点 33 与 44 之间的距离。

由于答案为 20302030,第二个查询解码为

d2′=(1+2030) mod 3=0d'_2 = (1 + 2030) \bmod 3 = 0

e2′=(1020+2030) mod 2000=1050e'_2 = (1020 + 2030) \bmod 2000 = 1050

因此,边 {1,2}\{1, 2\} 的权重被修改为 10501050。这使得顶点对 {1,4}\{1, 4\} 成为距离最大的顶点对,其距离为 20802080。

第三个查询解码为

d3′=(1+2080) mod 3=2d'_3 = (1 + 2080) \bmod 3 = 2

e3′=(890+2080) mod 2000=970e'_3 = (890 + 2080) \bmod 2000 = 970

由于边 {2,4}\{2, 4\} 的权重减小至 970970,此时距离最大的顶点对突然变为 {1,3}\{1, 3\},其距离为 20502050。

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

首页