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. Metasploit连接postgres数据库

    操作环境为Kali虚拟机 root@kali:~# apt-get install postgresql 启动服务 root@kali:~# service postgresql start [ ok ...

  2. Python 简单爬虫

    import os import time import webbrowser as web import random count = random.randint(20,40) j = 0 whi ...

  3. 【BZOJ1012】【JSOI2008】最大数

    难得的水题,应该是这题是T1吧,终于没看别人代码,然而思路还是看题解点了一下 原题: 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大 ...

  4. [hdu 3605]Escape

    这题的做法非常直观,却又非常不直观 先容许我吐一下槽吧~作者你的英语是读到火星上去了喵? 题目大体是说人类要移民,然后有 n 个人, m 个星球 每个人都有 m 个 0 . 1 数码表示他能否移民到该 ...

  5. Visual C++ 设置适合自己的解决方案目录结构

    Visual C++ 使用解决方案来管理项目,项目之间还可能有依赖关系,设置适合自己的解决方案目录结构,便于代码的管理.程序的发布. 下面开始一个虚拟解决方案设计:         假设此解决方案有应 ...

  6. Useful SQL Server Article

    http://blogs.technet.com/b/topsupportsolutions/archive/2013/11/06/top-support-solutions-for-microsof ...

  7. Verilog

    1.位拼接运算符{}

  8. MySQL 5.1.73升级为MySQL 5.5.35详解

    一.前言 二.概述 三.安装MySQL 5.1.73 四.升级为MySQL 5.5.35 五.总结 注,测试环境 CentOS 6.4 x86_64,MySQL 版本(5.1.73.5.5.35)目前 ...

  9. phpstorm编辑器智能提示框架代码

    按照上面的步骤就可以智能提示代码了!

  10. ASP.NET中JSON的序列化和反序列化

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介 ...