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数组可以轻易得到边没有被经过的概率
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 4 using namespace std; 5 6 const int maxn=1e5+5; 7 int siz[maxn]; //以节点i为根的子树的节点个数 8 int dep[maxn]; //节点深度 9 double pro[maxn]; //第i条边被经过的概率 10 int e[maxn][3]; 11 12 struct Edge 13 { 14 int to,next; 15 }; 16 Edge edge[maxn<<1]; 17 int head[maxn]; 18 int tot; 19 20 void addedge(int u,int v) 21 { 22 edge[tot].to=v; 23 edge[tot].next=head[u]; 24 head[u]=tot++; 25 } 26 27 void init() 28 { 29 memset(head,-1,sizeof head); 30 tot=1; 31 } 32 33 //方便计算的函数 34 double the_pro(double a,int n) 35 { 36 if(a<3) 37 return 0.0; 38 return (a*(a-1.0)*(a-2.0))/(n*(n-1.0)*(n-2.0)); 39 } 40 41 void swap(int &a,int &b) 42 { 43 a^=b; 44 b^=a; 45 a^=b; 46 } 47 48 void solve(int ); 49 void dfs(int ,int ); 50 51 int main() 52 { 53 init(); 54 int n; 55 scanf("%d",&n); 56 for(int i=1;i<n;i++) 57 { 58 scanf("%d %d %d",&e[i][0],&e[i][1],&e[i][2]); 59 addedge(e[i][0],e[i][1]); 60 addedge(e[i][1],e[i][0]); 61 } 62 solve(n); 63 return 0; 64 } 65 66 void solve(int n) 67 { 68 dep[1]=0; 69 dfs(1,0); 70 71 for(int i=1;i<=n;i++) 72 { 73 if(dep[e[i][0]]>dep[e[i][1]]) 74 swap(e[i][0],e[i][1]); 75 } 76 77 for(int i=1;i<n;i++) 78 { 79 pro[i]=1.0-the_pro(n-siz[e[i][1]],n)-the_pro(siz[e[i][1]],n); 80 pro[i]*=2.0; 81 } 82 double ans=0.0; 83 for(int i=1;i<n;i++) 84 { 85 ans+=pro[i]*e[i][2]; 86 } 87 //ans表示最开始的期望 88 int q; 89 scanf("%d",&q); 90 for(int j=1;j<=q;j++) 91 { 92 int i,w; 93 scanf("%d %d",&i,&w); 94 ans+=pro[i]*(w-e[i][2]); 95 //边权改变,期望跟着改变 96 printf("%.10f\n",ans); 97 e[i][2]=w; 98 } 99 return ; 100 } 101 102 void dfs(int u,int pre) 103 { 104 siz[u]=1; 105 for(int i=head[u];~i;i=edge[i].next) 106 { 107 int v=edge[i].to; 108 if(v==pre) 109 continue; 110 dep[v]=dep[u]+1; 111 dfs(v,u); 112 siz[u]+=siz[v]; 113 } 114 }