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,问 ...
随机推荐
- shell设置用户自己的环境变量
- python运行windows终端程序
其实是用python控制windows里的shell 1.windows有PowerShell,可以通过搜索打开,运行python不需要打开shell 2.用python里的subprocess函数, ...
- ORACLE 行转列的通用过程
--测试数据create table rowtocol_test asselect 2009 year,1 month,'部门1' dept,50000 expenditure from dualun ...
- Quartz.Net 任务调度之时间策略(4)
在复杂的业务逻辑中,cron可以灵活的制定时间策略 先说使用方法 ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trig ...
- 【leetcode】1012. Complement of Base 10 Integer
题目如下: Every non-negative integer N has a binary representation. For example, 5 can be represented a ...
- python环境变量
下载并升级更新pip python -m pip install -U pip 变量名:PY_HOME 变量值:python路径 path:win10加在最后(记得用;号隔开):win7加在前面记 ...
- 【Java】Java引用maven私服jar包及jar包提交私服问题
pom.xml中加入以下配置即可 1.引用私服jar包 <!-- 加载的是 第三方项目使用的jar包 --> <repositories> <repository> ...
- windows平台使用MongoDB shell 来连接 MongoDB 服务器并创建数据库
windows平台使用MongoDB shell 来连接 MongoDB 服务器并创建数据库 命令行进入MongoDB的bin目录运行mongod.exe mongod --dbpath c:\dat ...
- 移动端与pc端的区别 及 ios的 兼容性问题
前言:这里移动端主要指 hybrid app 中的H5页面.app 中对页面 样式和功能 的需求会更精细一点. 1.适配: 手机端的尺寸多样,3.5英寸的 iPhone4应该是最小的,只要考虑 兼容到 ...
- [CSP-S模拟测试]:长寿花(DP+组合数)
题目描述 庭院里有一棵古树.圣诞节到了,我想给古树做点装饰,给他一个惊喜.他会不会喜欢呢?这棵树可以分为$n$层,第$i$层有$a_i$个防治装饰品的位置,有$m$种颜色的装饰品可供选择.为了能让他喜 ...