Main topic
Give you a (n\) Point of the tree \ (t\), the edge of the right side.
Rule,\ (d (i,j) \) represents the sum of the Benquan on the point J path of points I.
To you \ (q\) times, each query format is \ (i, j\), indicating that the order will be sorted by the first (i\) edge of the right to modify to \ (j\), and asked to answer any of the three different points \ ( c_1,c_2,c_3\), the costs incurred \ (d (c_1,c_2) +d (c_1,c_3) + D (c_2,c_3) \) Expectations
The original surface d. New year Santa Network
Time limit per test
2 seconds
Memory limit per test
Megabytes
Input
Standard input
Output
Standard output
New year was coming in Tree world! In this world, as the name implies, there is n cities connected by N?-? 1 roads, and for any of the distinct cities there alw Ays exists a path between them. The cities is numbered by integers of 1 to N, and the roads is numbered by integers from 1 to n?-? 1. Let's define D (u,?v) as total length of roads in the path between City U and City v.
As an annual event, people in the Tree world repairs exactly one road per year. As a result, the length of one road decreases. It is already known so in the i-th year, the length of the Ri-th road was going to become WI, which was shorter than its L Ength before. Assume that the current year was year 1.
Three Santas is planning to give presents annually to all of the children in Tree world. In order to does, they need some preparation, so they is going to choose three distinct cities C1, C2, C3 and make Exa ctly one warehouse in each city. The k-th (1?≤?k?≤?3) Santa would 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 a Only-for-santa network! The cost needed to build this network equals to D (C1,?C2)? +?d (C2,?C3)? +?d (C3,?C1) dollars. Santas is too busy to find the best place, so they decided to choose c1,?c2,?c3 randomly uniformly over all triples of Di Stinct 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 a 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, space-separated integers RJ, WJ (1?≤?rj?≤?n?-? 1, 1?≤?wj?≤?103). It means in the j-th repair, the length of the Rj-th road becomes WJ. It is guaranteed this 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 the Tree world. The answer would be a considered correct if its absolute and relative error doesn ' t exceed?-? 6.
Examplesinput
Copy
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
Copy
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 is 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 was 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).
Exercises
Consider a choice of three points \ (a,b,c,\) will find the answer is \ (a->b, A->c, b->c\) traversed by the Benquan of twice times
Consider how many triples each edge might be selected. The equivalent of selecting three points in the subtree \ (t_1\) and \ (t_2\) that cut off this edge
Expect to divide by \ (C (n, 3) \) to
Notice will explode \ (long\) \ (long\), to \ (double\) side by side except to be over. Although the accuracy can be reduced, the \ (10^{-6}\ ) is (but) can be.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include < algorithm> #include <map> #include <cmath>inline long Long (long A, long B) {return a > B? a : b;} Inline Long long min (Long long A, long long B) {return a < b a:b;} Inline long long abs (long long x) {return x < 0-x:x;} inline void swap (long long &x, long long &y) {long long tmp = X;X = Y;y = tmp;} inline void read (long long &x) {x = 0;char ch = getchar (), c = ch; while (Ch < ' 0 ' | | ch > ' 9 ') c = ch, ch = getchar (); while (Ch <= ' 9 ' && ch >= ' 0 ') x = x * + ch-' 0 ', ch = getchar (); if (c = = '-') × =-X;} Const long Long INF = 0x3f3f3f3f;const long Long MAXN = 300000 + 10;struct edge{long long U, V, W, NXT, rank; Edge (Long long _u, long long _v, long long _w, long long _nxt, long long _rank) {rank = _rank;u = _u;v = _V;NXT = _nxt;w = _w;} Edge () {}}EDGE[MAXN << 1];long long HEAD[MAXN], CNT = 1, pos[maxn];inline void Insert (long long A, long long B, long long c, long long D) {edge[++ cnt] = Edge (A, B, C, Head[a], D), head[a] = cnt; edge[++ cnt] = Edge (b, A, C, Head[b], D), head[b] = cnt;} Long long N, Q, Size[maxn];long long C (Long long n, long long m) {if (M = = 1) return n; else return n * (n-1) >> 1;} void Dfs (Long long x, long long Pre) {size[x] = 1; For (long long pos = head[x];p os;pos = edge[pos].nxt) {Long long v = edge[pos].v; if (v = = pre) continue; DFS (v, x); ::p Os[edge[pos].rank] = pos; SIZE[X] + = Size[v]; }}double sum, mu;int main () {read (n); mu = 1; MU = n * (n-1)/2 * (n-2)/3; For (long long i = 1;i < n;++ i) {Long long tmp1, TMP2, Tmp3; Read (TMP1), read (TMP2), read (Tmp3); Insert (TMP1, TMP2, Tmp3, i); } DFS (1,-1); For (long long i = 1;i < n;++ i) {long long p = pos[i]; Sum + = 2 * EDGE[P].W * (C (Size[edge[P].V], 1) * C (N-SIZE[EDGE[P].V], 2) + C (SIZE[EDGE[P].V], 2) * C (N-SIZE[EDGE[P].V], 1))/MU; } read (q); For (long long i = 1;i <= q;++ i) {Long long tmp1, TMP2; Read (TMP1), read (TMP2); Long long p = pos[tmp1]; Sum + = 2 * (TMP2-EDGE[P].W) * (c (SIZE[EDGE[P].V], 1) * C (N-SIZE[EDGE[P].V], 2) + C (SIZE[EDGE[P].V], 2) * C (N-SIZE[EDGE[P].V], 1))/MU; EDGE[P].W = TMP2; printf ("%.10lf\n", (double) sum); } return 0;}
Codeforces 500D. New year Santa Network