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. Codeforces Round #118 (Div. 2)

    A. Comparing Strings 判断不同的位置个数以及交换后是否相等. B. Growing Mushrooms 模拟. C. Plant 矩阵+快速幂 D. Mushroom Scient ...

  2. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  3. HDU5889 Barricade(最短路)(网络流)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. phpwind8.7升级9.0.1过程(四)20130207升级到20141228

    每一次升级前都要注意备份 1.网站根目录的所有文件 2.网站的数据库 根据phpwind官方教程 更新到20130702版本成功并备份 更新到20140428版本成功并备份 20141228版本的更新 ...

  5. Python字符串操作

    isalnum()判断是否都是有效字符串 >>> ev1 = 'evilxr' >>> ev2 = 'ev1il2xr3' >>> ev3 = ' ...

  6. netsh winsock reset

    最近要在虚拟机上安装 git ,然后托管一个项目,搞得我是焦头烂额.今天下午我卸载了vmware workstation ,但是奇怪的事就发生了,我的google chrome 浏览器没法浏览网页了. ...

  7. css animation让图标不断旋转

    @keyframes rotating{from{transform:rotate(0)}to{transform:rotate(360deg)}} animation:rotating 1.2s l ...

  8. Google Java Style Guide

    https://google.github.io/styleguide/javaguide.html   Table of Contents 1 Introduction 1.1 Terminolog ...

  9. Android 异步加载解决方案

    Android的Lazy Load主要体现在网络数据(图片)异步加载.数据库查询.复杂业务逻辑处理以及费时任务操作导致的异步处理等方面.在介绍Android开发过程中,异步处理这个常见的技术问题之前, ...

  10. 关于ttserver, mongodb, couchbase. ssdb ,tair, leveldb的一点使用体验

    2年前使用的ttserver,性能很高,支持分布式,但稳定性不足,当存储容量达到亿级的时间经常会出现无法插入的情况,而且不知道是什么原因造成的错误,重启后也无济于事,只好重启开新库. 单库写入性能 2 ...