D. 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,bili (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 integersrjwj (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.

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 to d(1, 2) + d(2, 3) + d(3, 1).

这题赛中没做出来有点可惜呀(不然黄了)。题意是给出一棵带权树。

问你在树上任意选3个点 c1 , c2 ,c3 的 d(c1,c2) + d(c2,c3) + d(c1 ,c3 ) 的期望。

因为每一颗树上面的边都是桥。

只要计算每一条边在所有情况下[ C(3 , n ) ]使用的次数cnt便可以知道总路程d = sigma( cnt[i] *w[i] )

那么exp = d / C(3 ,n ) 。

q次询问更改边权就是减去之前的边期望, 加上新的边权求出来的期望 。

注意要用 , cnt[i]*w[i] / C(3 , n ) 处理好边, 即边期望。

若果先求出总路程再除C(3 , n )的话数据过大会溢出(挂大数据呀QAQ)。

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define X first
#define Y second
const int N = ; int n , cnt[N];
double W[N] , Cn3, res; vector<pii>g[N]; int dfs( int u , int fa ){
int tot = ;
for( int i = ; i < g[u].size(); ++i ){
int v = g[u][i].X , id = g[u][i].Y;
if( v == fa ) continue ;
cnt[id] = dfs( v , u );
tot += cnt[id];
}
return tot + ;
} double cal( int id ){
double a = cnt[id] , b = n - cnt[id];
return 2.0*W[id]/Cn3*(a*(a-)/2.0*b + b*(b-)/2.0*a);
} int main()
{
int u , v ;
cin >> n ;
Cn3 = 1.0*n*(n-)*(n-)/ , res = ;
for( int i = ; i < n ; ++i ){
scanf("%d%d%lf",&u,&v,&W[i]);
g[u].push_back(pii(v,i));
g[v].push_back(pii(u,i));
}
int m = dfs(,);
for( int i = ; i < n ; ++i ) res += cal(i);
int q ; scanf("%d",&q);
while(q--){
int x ;double y ; scanf("%d%lf",&x,&y);
res -= cal(x); W[x] = y ;res += cal(x);
printf("%.9lf\n",res);
}
}

Codeforces 500D New Year Santa Network(树 + 计数)的更多相关文章

  1. Codeforces 500D. New Year Santa Network

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

  2. CF 500D New Year Santa Network tree 期望 好题

    New Year is coming in Tree World! In this world, as the name implies, there are n cities connected b ...

  3. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

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

  5. cf500D New Year Santa Network

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

  6. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  7. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  8. Codeforces 633C Spy Syndrome 2 | Trie树裸题

    Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...

  9. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

随机推荐

  1. 二、RabbitMQ操作

    1.RabbitMQ发送与接收. 2.RabbitMQ发送与接收. 3.RabbitMQ发送与接收.

  2. ltp-ddt lmbench

    ltp-ddt lmbench args='' # getopt fails, set help optionif [ $? -ne 0 ] ; then        H="help&qu ...

  3. html5 固定边栏滚动特效

    <script src="https://code.jquery.com/jquery.js"></script> //引入jquery <scrip ...

  4. 常用jstl

    求list中某一值的和 <c:set var="total" value="${0}" /> <c:forEach var="tLi ...

  5. Web核心之Servlet接口

    Servlet(server applet)概念: Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务 ...

  6. shell脚本特殊变量与变量子串相关知识

    一.shell脚本特殊变量 1.shell中常用特殊位置变量说明: $0 获取当前执行的shell脚本的文件名,如果执行脚本包含了路径,那么就包含了脚本路径 $n 获取当前执行的shell脚本的第n个 ...

  7. CVE-2017-0213 | 记一次失败的提权经历

    环境: CVE-2017-0213下载 提权步骤: 提权失败.... 好迷啊,,,,事后查了一下补丁 我的wind7上也没装啊,然后防火墙也是关闭的 迷了迷了....

  8. vue数据渲染、条件判断及列表循环

    1.数据渲染  {{msg}} <template> <div id="app"> {{msg}} </div> </template&g ...

  9. 2018-2019-2 20175120 实验五《Java网络编程》实验报告

    实验报告封面 课程:Java程序设计 班级:1751班 姓名:彭宇辰 学号:20175120 指导教师:娄嘉鹏 实验日期:2019年5月26日 实验时间:13:10 - 15:25 实验序号:20 实 ...

  10. 第六周学习总结&实验报告四

    一.实验目的 (1)掌握类的继承 (2)变量的继承和覆盖,方法的继承,重载和覆盖的实现: 二.实验的内容 (1)根据下面的要求实现圆类Circle. 1.圆类Circle的成员变量:radius表示圆 ...