CF500D.New Year Santa Network

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

New Year is coming in Tree World! In this world, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to n, and the roads are numbered by integers from 1 to n - 1. Let's define d(u, v) as total length of roads on the path between city u and city v.

As an annual event, people in Tree World repairs exactly one road per year. As a result, the length of one road decreases. It is already known that in the i-th year, the length of the r__i-th road is going to become w__i, which is shorter than its length before. Assume that the current year is year 1.

Three Santas are planning to give presents annually to all the children in Tree World. In order to do that, they need some preparation, so they are going to choose three distinct cities _c_1, _c_2, _c_3 and make exactly one warehouse in each city. The k-th (1 ≤ k ≤ 3) Santa will take charge of the warehouse in city c__k.

It is really boring for the three Santas to keep a warehouse alone. So, they decided to build an only-for-Santa network! The cost needed to build this network equals to d(_c_1, _c_2) + d(_c_2, _c_3) + d(_c_3, _c_1) dollars. Santas are too busy to find the best place, so they decided to choose _c_1, _c_2, _c_3 randomly uniformly over all triples of distinct numbers from 1 to n. Santas would like to know the expected value of the cost needed to build the network.

However, as mentioned, each year, the length of exactly one road decreases. So, the Santas want to calculate the expected after each length change. Help them to calculate the value.

新年即将来到树形世界!在这个世界中,顾名思义,共有 nn 座城市,由 n−1n-1 条道路连接;且对任意两座不同的城市,其间总存在一条路径。城市编号为 11 至 nn 的整数,道路编号为 11 至 n−1n-1 的整数。我们定义 d(u, v)d(u,\,v) 为城市 uu 与城市 vv 之间路径上所有道路长度的总和。

作为一年一度的活动,树形世界的人们每年恰好修复一条道路。因此,某条道路的长度会减小。已知在第 ii 年,第 rir_i 条道路的长度将变为 wiw_i,该值严格小于其此前的长度。假设当前年份为第 11 年。

三位圣诞老人计划每年向树形世界的所有儿童赠送礼物。为此,他们需要进行一些准备工作,故将选择三座互不相同的城市 c1, c2, c3c_1,\,c_2,\,c_3,并在每座城市中各建立一座仓库。第 kk 位(1≤k≤31\le k\le 3)圣诞老人将负责城市 ckc_k 中的仓库。

三位圣诞老人独自看管一座仓库实在太过乏味。因此,他们决定共建一个“仅限圣诞老人使用”的网络!构建该网络所需的成本为 d(c1, c2)+d(c2, c3)+d(c3, c1)d(c_1,\,c_2) + d(c_2,\,c_3) + d(c_3,\,c_1) 美元。圣诞老人太忙,无暇寻找最优选址方案,因此决定在 11 至 nn 中所有互不相同的三元组 (c1, c2, c3)(c_1,\,c_2,\,c_3) 上均匀随机地选取。圣诞老人希望知道构建该网络所需成本的期望值。

然而,如前所述,每年恰好有一条道路的长度会减小。因此,圣诞老人希望在每次长度变更后重新计算该期望值。请帮助他们完成这一计算。

输入格式

The first line contains an integer n (3 ≤ n ≤ 105) — the number of cities in Tree World.

Next n - 1 lines describe the roads. The i-th line of them (1 ≤ i ≤ n - 1) contains three space-separated integers a__i, b__i, l__i (1 ≤ a__i, b__i ≤ n, a__i ≠ b__i, 1 ≤ l__i ≤ 103), denoting that the i-th road connects cities a__i and b__i, and the length of i-th road is l__i.

The next line contains an integer q (1 ≤ q ≤ 105) — the number of road length changes.

Next q lines describe the length changes. The j-th line of them (1 ≤ j ≤ q) contains two space-separated integers r__j, w__j (1 ≤ r__j ≤ n - 1, 1 ≤ w__j ≤ 103). It means that in the j-th repair, the length of the r__j-th road becomes w__j. It is guaranteed that w__j is smaller than the current length of the r__j-th road. The same road can be repaired several times.

第一行包含一个整数 nn(3≤n≤1053 \leq n \leq 10^5)—— 表示 Tree World 中的城市数量。

接下来的 n−1n-1 行描述道路。其中第 ii 行(1≤i≤n−11 \leq i \leq n-1)包含三个用空格分隔的整数 aia_i、bib_i、lil_i(1≤ai,bi≤n1 \leq a_i, b_i \leq n,ai≠bia_i \neq b_i,1≤li≤1031 \leq l_i \leq 10^3),表示第 ii 条道路连接城市 aia_i 和 bib_i,且该道路的长度为 lil_i。

下一行包含一个整数 qq(1≤q≤1051 \leq q \leq 10^5)—— 表示道路长度修改操作的次数。

接下来的 qq 行描述长度修改操作。其中第 jj 行(1≤j≤q1 \leq j \leq q)包含两个用空格分隔的整数 rjr_j、wjw_j(1≤rj≤n−11 \leq r_j \leq n-1,1≤wj≤1031 \leq w_j \leq 10^3)。它表示在第 jj 次维修中,第 rjr_j 条道路的长度变为 wjw_j。保证 wjw_j 小于第 rjr_j 条道路当前的长度。同一条道路可能被多次维修。

输出格式

Output q numbers. For each given change, print a line containing the expected cost needed to build the network in Tree World. The answer will be considered correct if its absolute and relative error doesn't exceed 10 - 6.

输出 q 个数字。对于每次给定的修改,打印一行,包含在“树世界”(Tree World)中构建该网络所需的期望花费。若答案的绝对误差和相对误差均不超过 10−610^{-6},则视为正确。

输入输出样例

  • 输入#1

    3
    2 3 5
    1 3 3
    5
    1 4
    2 2
    1 2
    2 1
    1 1

    输出#1

    14.0000000000
    12.0000000000
    8.0000000000
    6.0000000000
    4.0000000000
  • 输入#2

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

    输出#2

    19.6000000000
    18.6000000000
    16.6000000000
    13.6000000000
    12.6000000000

说明/提示

Consider the first sample. There are 6 triples: (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1). Because n = 3, the cost needed to build the network is always d(1, 2) + d(2, 3) + d(3, 1) for all the triples. So, the expected cost equals to d(1, 2) + d(2, 3) + d(3, 1).

考虑第一个样例。共有 6 个三元组:(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)。由于 n = 3n = 3,对所有三元组而言,构建网络所需的代价恒为 d(1, 2) + d(2, 3) + d(3, 1)d(1, 2) + d(2, 3) + d(3, 1)。因此,期望代价等于 d(1, 2) + d(2, 3) + d(3, 1)d(1, 2) + d(2, 3) + d(3, 1)。

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

首页