Codeforces 500D New Year Santa Network(树 + 计数)
D. New Year Santa Network
2 seconds
256 megabytes
standard input
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.
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,bi, li (1 ≤ ai, bi ≤ n, ai ≠ 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 integersrj, wj (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 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.
3
2 3 5
1 3 3
5
1 4
2 2
1 2
2 1
1 1
14.0000000000
12.0000000000
8.0000000000
6.0000000000
4.0000000000
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
19.6000000000
18.6000000000
16.6000000000
13.6000000000
12.6000000000
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(树 + 计数)的更多相关文章
- Codeforces 500D. New Year Santa Network
题目大意 给你一颗有\(n\)个点的树\(T\),边上有边权. 规定,\(d(i,j)\)表示点i到点j路径上的边权之和. 给你\(q\)次询问,每次询问格式为\(i, j\),表示将按输入顺序排序的 ...
- 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 ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- 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 ...
- cf500D New Year Santa Network
D. New Year Santa Network time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)
[Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces 633C Spy Syndrome 2 | Trie树裸题
Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
随机推荐
- 【JAVA】eclipse-使用入门及常用快捷键
目录 下载与安装 HelloWorld 新建项目 视图与视窗 快捷键 个性化设置 导入项目 jar包 下载与安装 下载 网址:官网下载 注意: 下载javaee版 注意与本机的java环境相匹配,32 ...
- elasticsearch 深入 —— 相关度控制
控制相关度 处理结构化数据(比如:时间.数字.字符串.枚举)的数据库, 只需检查文档(或关系数据库里的行)是否与查询匹配. 布尔的是/非匹配是全文搜索的基础,但不止如此,我们还要知道每个文档与查询的相 ...
- elasticsearch索引清理脚本shell
es-index-clear.sh: #!/bin/bash#----------------------------------------------# Module: es-index-clea ...
- ubuntu-12.04.5-desktop-amd64 安装vmwaretools
百度文库地址:https://wenku.baidu.com/view/7c1cd211a216147917112820.html 注意:一定要把此文档中的vmwaretools 版本号换成你自己下载 ...
- 数据库_PXC群集与存储引擎
1. PXC介绍与群集搭建; 2.数据存储引擎. 一, PXC介绍 1.介绍 PXC(Percona XtraDB Cluster)基于Galara的一台开源软件,应用于解决mysql的高可用集群问题 ...
- 从零开始之uboot、移植uboot2017.01(一、移植前的准备)
手边的是一个S5PV210的开发板,想尝试移植一个比较新的uboot 下载最新版本uboot2018. ftp://ftp.denx.de/pub/u-boot/ 编译器下载 http://www.v ...
- Servlet接口和web.xml配置文件初识
一.Java Web核心Servlet 1.什么是Servlet? Servlet是运行在服务器端的Java小程序,是sun公司提供的一套规范,用来处理客户端请求.响应给浏览器的动态资源.但Servl ...
- GSL--GNU Scientific Library 小记
摘自http://qianjigui.iteye.com/blog/847612 GSL(GNU Scientific Library)是一个 C 写成的用于科学计算的库,下面是一些相关的包 Desi ...
- kill命令的几种信号
1 HUP: hangup 2 INIT: 相当于 Ctrl + c 9 KILL 15 TERM: Terminate (kill 的默认信号) 18 CONT: Continue (从STOP信号 ...
- shell脚本条件测试与比较
1.条件测试常用语法 test 测试表达式 利用test命令进行条件测试表达式,test命令与测试表达式之间至少有一个空格 [ 测试表达式 ] 通过[ ]中括号进行条件测试表达式,[]中括号边界与测试 ...