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. springboot的jar包部署

    由于springboot常用war包部署,改为cloud开发模式多端口情况下,部署反而不习惯 毕竟,war包要不要项目名访问都必须放在tomcat的root目录下 而此目录限制只能放置一个项目,并且登 ...

  2. weblogicjsp编译:查看编译后的java中间代码

    转自:https://www.xuebuyuan.com/1069484.html 运行自己配置的web应用,往往只能看见weblogic编译之后的class文件.而看不见编译前的java的文件.为了 ...

  3. Failed to load C:\ProgramFilesTwo\Android\sdk\build-tools\27.0.3\lib\dx.jar

    Eclipse遇到如下错误: Failed to load C:\ProgramFilesTwo\Android\sdk\build-tools\27.0.3\lib\dx.jar 原因: eclip ...

  4. C语言字符串复制

    strcpy(arg1,arg2);//将arg2内容赋值到arg1 strncpy(arg1,arg2,size);//赋值多少由size决定,如果要截取某一部分,可以将arg2指针进行arg2+x ...

  5. jenkins持续集成(三): jenkins配置邮件通知

    完成基于jenkins的持续集成部署后,任务构建执行完成,测试结果需要通知到相关人员.这篇博客,介绍如何在jenkins中配置邮件通知的方法... 一.安装邮件插件 由于Jenkins自带的邮件功能比 ...

  6. Oracle 设置自启动

    1. 环境准备 1.1 系统 操作系统:CentOS 7(64位) 1.2 工具/软件 已安装完成的Oracle11g(64位):  创建数据库实例,本文中数据库实例名:test:$ORACLE_SI ...

  7. python之pandas模块高级用法

    一 agg,聚合,可以使用内置的函数 >>> import pandas as pd >>> import numpy as np >>> pp ...

  8. Linux命令"ls"进阶说明

    pwd:the current working directory cd -: return to the previous working directory Filenames that begi ...

  9. 通过ssh访问虚拟机中的ubuntu系统

    首先把 network 连接方式由 NAT 改为 Bridge Adapter,这样虚拟机中的 ubuntu 就可以有独立的 IP 地址. 安装 openssh: sudo apt-get insta ...

  10. Python 递归算法指归

    1. 递归概述 递归( recursion)是一种编程技巧,某些情况下,甚至是无可替代的技巧.递归可以大幅简化代码,看起来非常简洁,但递归设计却非常抽象,不容易掌握.通常,我们都是自上而下的思考问题, ...