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 aibili(1 ≤ ai, bi ≤ nai ≠ 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 rjwj(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 thatwj 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.

Sample test(s)
input
3
2 3 5
1 3 3
5
1 4
2 2
1 2
2 1
1 1
output
14.0000000000
12.0000000000
8.0000000000
6.0000000000
4.0000000000
input
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
output
19.6000000000
18.6000000000
16.6000000000
13.6000000000
12.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 tod(1, 2) + d(2, 3) + d(3, 1).

题意:

一棵树,n个节点,编号为1~n,n-1条边按输入的顺序编号为1~n-1,给出n-1条边的权值

在树上任意选择3个点c1,c2,c3(不互相同),则连接这3个点的总花费:

dis(c1,c2)+dis(c1,c3)+dis(c2,c3)

注意:3个点的选择是随机的

接着q个改变,

每一个改变给出i w:把第i条边的权值改为w

每一个改变后,输出现在选择3个点总花费的期望。

思路:

一共有n*(n-1)*(n-2)种情况

在任意一种情况中,一条边要么没有被经过,要么被经过了2次

对于每一条边对期望的贡献=该边被经过的概率*该边的边长

而总期望=所有边的贡献之和

被经过的概率=1.0-没有被经过的概率

对于边e=(u,v)没有被经过,3个点要么都在u一侧,要么都在v一侧,根据siz数组可以轻易得到边没有被经过的概率

 #include<cstdio>
#include<cstring> using namespace std; const int maxn=1e5+;
int siz[maxn]; //以节点i为根的子树的节点个数
int dep[maxn]; //节点深度
double pro[maxn]; //第i条边被经过的概率
int e[maxn][]; struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot; void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void init()
{
memset(head,-,sizeof head);
tot=;
} //方便计算的函数
double the_pro(double a,int n)
{
if(a<)
return 0.0;
return (a*(a-1.0)*(a-2.0))/(n*(n-1.0)*(n-2.0));
} void swap(int &a,int &b)
{
a^=b;
b^=a;
a^=b;
} void solve(int );
void dfs(int ,int ); int main()
{
init();
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d %d %d",&e[i][],&e[i][],&e[i][]);
addedge(e[i][],e[i][]);
addedge(e[i][],e[i][]);
}
solve(n);
return ;
} void solve(int n)
{
dep[]=;
dfs(,); for(int i=;i<=n;i++)
{
if(dep[e[i][]]>dep[e[i][]])
swap(e[i][],e[i][]);
} for(int i=;i<n;i++)
{
pro[i]=1.0-the_pro(n-siz[e[i][]],n)-the_pro(siz[e[i][]],n);
pro[i]*=2.0;
}
double ans=0.0;
for(int i=;i<n;i++)
{
ans+=pro[i]*e[i][];
}
//ans表示最开始的期望
int q;
scanf("%d",&q);
for(int j=;j<=q;j++)
{
int i,w;
scanf("%d %d",&i,&w);
ans+=pro[i]*(w-e[i][]);
//边权改变,期望跟着改变
printf("%.10f\n",ans);
e[i][]=w;
}
return ;
} void dfs(int u,int pre)
{
siz[u]=;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==pre)
continue;
dep[v]=dep[u]+;
dfs(v,u);
siz[u]+=siz[v];
}
}

CF 500D New Year Santa Network tree 期望 好题的更多相关文章

  1. Good Bye 2014 D. New Year Santa Network 图论+期望

    D. New Year Santa Network   New Year is coming in Tree World! In this world, as the name implies, th ...

  2. Codeforces 500D New Year Santa Network(树 + 计数)

    D. New Year Santa Network time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. Codeforces 500D. New Year Santa Network

    题目大意 给你一颗有\(n\)个点的树\(T\),边上有边权. 规定,\(d(i,j)\)表示点i到点j路径上的边权之和. 给你\(q\)次询问,每次询问格式为\(i, j\),表示将按输入顺序排序的 ...

  4. cf500D New Year Santa Network

    D. New Year Santa Network time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  5. D. New Year Santa Network 解析(思維、DFS、組合、樹狀DP)

    Codeforce 500 D. New Year Santa Network 解析(思維.DFS.組合.樹狀DP) 今天我們來看看CF500D 題目連結 題目 給你一棵有邊權的樹,求現在隨機取\(3 ...

  6. CF 600E. Lomsat gelral(dsu on tree)

    解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostr ...

  7. 【codeforces 500D】New Year Santa Network

    [题目链接]:http://codeforces.com/problemset/problem/500/D [题意] 有n个节点构成一棵树; 让你随机地选取3个不同的点a,b,c; 然后计算dis(a ...

  8. cf 500 D. New Year Santa Network

    直接按边分,2个点在边的一边,1个在另一边,组合出来就是这个边对答案的贡献,权值换了就再重新算个数而已. #include <bits/stdc++.h> #define LL long ...

  9. (中等) CF 555E Case of Computer Network,双连通+树。

    Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...

随机推荐

  1. DAG上的动态规划之嵌套矩形

    题意描述:有n个矩形,每个矩形可以用两个整数a.b描述,表示它的长和宽, 矩形(a,b)可以嵌套在矩形(c,d)当且仅当a<c且b<d, 要求选出尽量多的矩形排成一排,使得除了最后一个外, ...

  2. php支付接口,代付、感悟

    支付接口: 1.验证用户登录信息 2.验证参数.用加密串来匹配,开信息是否被篡改 3.如果有必要可以仿造购物城 建立购物车列表 4.建立和请求方的关联表 5.进行订单生成.支付流程.各种判断.验证. ...

  3. phpwind8.7升级9.0.1过程(二)8.7正式升级9.0

    首先备份 1. 给本地做备份将网站根目录下面的所有文件先做备份. 2. 给本地数据库做备份 3. 了解升级的具体过程 phpwind8.7升级到9.0需要首先升级到phpwind9.0的原生版本即:2 ...

  4. img元素底部有空白间距的问题

    <div style="width:100px;height:100px"><img src="./1.jpg"></div> ...

  5. sed详解

    1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后 ...

  6. Python元组的简单介绍

    1.实际上元组是跟列表非常相近的另一种容器类型.元组和列表看上去的不同的一点是元组用圆括号而列表用方括号.而在功能上,元组是一种不可变的类型.正是因为这个原因,元组可以做一些列表不可以做的事情,比如用 ...

  7. python--类方法、对象方法、静态方法

    1.我们已经讨论了类/对象可以拥有像函数一样的方法,这些对象方法与函数的区别只是一个额外的self变量 # -*- coding:utf-8 -*- #!/usr/bin/python # Filen ...

  8. JS页面间传值

    一:JavaScript静态页面值传递之URL篇 能过URL进行传值.把要传递的信息接在URL上. 例子: 参数传出页面Post.htm—>   <input type="tex ...

  9. noip2003复赛普及组第一题——乒乓球

    /*======================================================================= 题一.乒乓球(Table.pas) [问题背景]国际 ...

  10. OpenJudge计算概论-求满足条件的3位数

    /*======================================================================== 求满足条件的3位数 总时间限制: 1000ms 内 ...