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 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 integers rj, 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 thatwj 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 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 期望 好题的更多相关文章
- 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 ...
- 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 ...
- Codeforces 500D. New Year Santa Network
题目大意 给你一颗有\(n\)个点的树\(T\),边上有边权. 规定,\(d(i,j)\)表示点i到点j路径上的边权之和. 给你\(q\)次询问,每次询问格式为\(i, j\),表示将按输入顺序排序的 ...
- cf500D New Year Santa Network
D. New Year Santa Network time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- D. New Year Santa Network 解析(思維、DFS、組合、樹狀DP)
Codeforce 500 D. New Year Santa Network 解析(思維.DFS.組合.樹狀DP) 今天我們來看看CF500D 題目連結 題目 給你一棵有邊權的樹,求現在隨機取\(3 ...
- CF 600E. Lomsat gelral(dsu on tree)
解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostr ...
- 【codeforces 500D】New Year Santa Network
[题目链接]:http://codeforces.com/problemset/problem/500/D [题意] 有n个节点构成一棵树; 让你随机地选取3个不同的点a,b,c; 然后计算dis(a ...
- cf 500 D. New Year Santa Network
直接按边分,2个点在边的一边,1个在另一边,组合出来就是这个边对答案的贡献,权值换了就再重新算个数而已. #include <bits/stdc++.h> #define LL long ...
- (中等) CF 555E Case of Computer Network,双连通+树。
Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...
随机推荐
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- hive学习笔记_hive的表创建
创建hive表注意事项 一.表分隔符必须与读取的数据文件一致,比如例子的分隔符为 '\t'(制表符),hive下默认分隔符是制表符. 二.最好指定分区作为数据之间的区分. 三.创建完表可以desc+表 ...
- ZOJ-3946 Highway Project (最短路)
题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省 ...
- 错误:无法访问android.app.Activity 找不到android.app.Activity的类文件
视频里面在工程ndk22/bin/classes中 运行javah com.cn.ndk22.Ndk22.Activity ,出现了.h文件 但是我在bin/classes目录中运行javah 时出 ...
- 修改host文件屏蔽视频广告和网站
很多时候我们会需要屏蔽一些网站或者广告,类似XX网站,下木马病毒的网站,或者破解软件的时候.我们可以使用一些软件屏蔽,我这里是用windows系统自带的hosts文件来屏蔽的.这个文件有点类似精简版的 ...
- C++实现水波纹、火焰和血浆效果
点击这里查看原文 Code Project着火了! 整个工程有三个类,它们可以让你在图象上添加一些很酷的效果. 我把这些文件都放到我的代码压缩包里面了,并且做了一个小工程来让一些人使用起来更方便,但是 ...
- NLTK中的词性
NOUN n,VERB v ,ADJ a, ADV r, ADJ_SAT s NOUN: [('s', ''), ('ses', 's'), ('ves', 'f'), ('xes', 'x' ...
- python生态环境
https://docs.python.org/2.7/ 这是文档页 https://docs.python.org/2.7/download.html 2.7的文档下载地址,下载一个包,所有文档就都 ...
- Openjudge计算概论——数组逆序重放【递归练习】
/*===================================== 数组逆序重放 总时间限制:1000ms 内存限制:65536kB 描述 将一个数组中的值按逆序重新存放. 例如,原来的顺 ...
- 【转】纯CSS设置Checkbox复选框控件的样式
Checkbox复选框是一个可能每一个网站都在使用的HTML元素,但大多数人并不给它们设置样式,所以在绝大多数网站它们看起来是一样的.为什么不把你的网站中的Checkbox设置一个与众不同的样式,甚至 ...