【Good Bye 2014D】【期望的线性可加 基本元素累计】New Year Santa Network 树上取3点,路径权值之和的期望

来源:互联网 发布:背身护球 知乎 编辑:程序博客网 时间:2024/06/06 19:01

New Year Santa Network
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ri-th road is going to become wi, 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 c1, c2, c3 and make exactly one warehouse in each city. The k-th (1 ≤ k ≤ 3) Santa will take charge of the warehouse in city ck.

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(c1, c2) + d(c2, c3) + d(c3, c1) dollars. Santas are too busy to find the best place, so they decided to choose c1, c2, c3 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.

Input

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 ai, bi, li (1 ≤ ai, bi ≤ n,ai ≠ bi, 1 ≤ li ≤ 103), denoting that the i-th road connects cities ai and bi, and the length of i-th road is li.

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 rj, wj (1 ≤ rj ≤ n - 1,1 ≤ wj ≤ 103). It means that in the j-th repair, the length of the rj-th road becomes wj. It is guaranteed that wj is smaller than the current length of the rj-th road. The same road can be repaired several times.

Output

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.

Examples
input
32 3 51 3 351 42 21 22 11 1
output
14.000000000012.00000000008.00000000006.00000000004.0000000000
input
61 5 35 3 26 1 71 4 45 2 351 22 13 54 15 2
output
19.600000000018.600000000016.600000000013.600000000012.6000000000
Note

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).


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 1e5+10, M = 2e5+10, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int n;int x, y, z;vector<int>a[N];int first[N]; int id;int w[M], c[M], nxt[M];int son[N],up[N],dn[N];void ins(int x, int y, int z){    w[++id] = y;    c[id] = z;    nxt[id] = first[x];    first[x] = id;}LL C3(LL x){    return x*(x - 1)*(x - 2) / 6;}LL C2(LL x){    return x*(x - 1) / 2;}double ans;void dp(int x,int fa){    son[x] = 1;    for (int z = first[x]; z;z=nxt[z])    {        int y = w[z];        if (y == fa)continue;        dp(y, x);        son[x] += son[y];        int zz = z >> 1;        dn[zz] = son[y];        up[zz] = (n - son[y]);        ans += (C2(dn[zz])*up[zz] + C2(up[zz])*dn[zz]) * (double)c[z];    }}int main(){    while (~scanf("%d", &n))    {        MS(first, 0); id = 1;        for (int i = 1; i < n; ++i)        {            scanf("%d%d%d", &x, &y, &z);            ins(x, y, z);            ins(y, x, z);        }        ans = 0;        dp(1, 0);        int q; scanf("%d", &q);        double P = 2.0 / C3(n);        while (q--)        {            int o, l; scanf("%d%d", &o, &l);            ans -= (C2(dn[o])*up[o] + C2(up[o])*dn[o]) * (double)(c[o << 1] - l);            c[o << 1] = l;            printf("%.15f\n", ans * P);        }    }    return 0;}/*【trick&&吐槽】有的概率性题目,我们从最基本的量出发,反而是最行之有效的方法。【题意】有n(1e5)个城市和n-1条边,构成一棵树。同时我们会有q(1e5)次修改操作。对于每次操纵,我们把一条边的边权减小l。每次操作之后,我们任意选3个点,问你这三个点之间3条路径的距离之和的期望是多少。【类型】脑洞 数学【分析】这道题一直想着树形DP,题目的展开和解决就变得很难。于是,我们为何不用概率学来考虑解决问题呢?首先,我们全部都不考虑顺序。n个点选出3个,方案数为C(n,3)然后,我们不妨以1为树根,然后研究每条边。我们可以通过dfs,得到每条边的上下各有多少个点。假如说在一条边i的上面有up[i]个点,下面有dn[i]个点。那么,我们就有C(up[i],1)*C(dn[i],2)+C(up[i],2)*C(dn[i],1)种方案选到这条边于是,我们累计每条边被选中的方案*边权,再/=方案数,答案就出来啦!然而,我这样算出来的答案有问题,只有实际答案的一半,为什么呢?因为有C(up[i],1)*C(dn[i],2)+C(up[i],2)*C(dn[i],1)种方案选到这条边而每个方案中,这个边权都被我们走了2次。于是最后再*2就可以啦【时间复杂度&&优化】O(n+q)【数据】*/


0 0
原创粉丝点击