Good Bye 2014 D --- New Year Santa Network, 2014d ---

Source: Internet
Author: User

Good Bye 2014 D --- New Year Santa Network, 2014d ---
D. New Year Santa Networktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

New Year is coming in Tree World! In this world, as the name implies, there areNCities connectedNTables-Tables 1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1N, And the roads are numbered by integers from1NAccept-limit 1. Let's defineD(U, Bytes,V) As total length of roads on the path between cityUAnd cityV.

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 inI-Th year, the length ofRI-Th road is going to becomeWI, Which is shorter than its length before. Assume that the current year is year1.

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 citiesC1,C2,C3 and make exactly one warehouse in each city.K-Th (1 digit ≤ secondKMinimum ≤ limit 3) Santa will take charge of the warehouse in cityCK.

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 equalsD(C1, bytes,C2) accept + acceptD(C2, bytes,C3) accept + acceptD(C3, bytes,C1) dollars. Santas are too busy to find the best place, so they decided to chooseC1, bytes,C2, bytes,C3 randomly uniformly over all triples of distinct numbers from1N. Santas wocould 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 an integerN(3 cores ≤ CoresNLimit ≤ limit 105)-the number of cities in Tree World.

NextNResponse-Generation 1 lines describe the roads.I-Th line of them (1 operator ≤ OperatorILimit ≤ limitNCounter-partitions 1) contains three space-separated integersAI,BI,LI(1 digit ≤ DigitAI, Bytes,BILimit ≤ limitN,AI  =BI, 1 limit ≤ limitLILimit ≤ limit 103), denoting thatI-Th road connects citiesAIAndBI, And the lengthI-Th road isLI.

The next line contains an integerQ(1 digit ≤ DigitQLimit ≤ limit 105)-the number of road length changes.

NextQLines describe the length changes.J-Th line of them (1 operator ≤ OperatorJLimit ≤ limitQ) Contains two space-separated integersRJ,WJ(1 digit ≤ DigitRJLimit ≤ limitNMaximum-random, 1, medium ≤ maximumWJLimit ≤ limit 103). It means that inJ-Th repair, the length ofRJ-Th road becomesWJ. It is guaranteed thatWJIs smaller than the current length ofRJ-Th road. The same road can be retried red several times.

Output

OutputQNumbers. 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 exceed10Upload-Snapshot 6.

Sample test (s) Input
32 3 51 3 351 42 21 22 11 1
Output
14.000000000012.00000000008.00000000006.00000000004.0000000000
Input
61 5 35 3 26 1 71 4 45 2 351 22 13 54 15 2
Output
19.600000000018.600000000016.600000000013.600000000012.6000000000
Note

Consider the first sample. there are 6 triples: (1, California 2, California 3), California (1, California 3, California 2), California (2, California 1, California 3 ), second (2, second 3, second 1), second (3, second 1, second 2), second (3, second 2, second 1 ). becauseNRequired = required 3, the cost needed to build the network is alwaysD(1, second 2) second + secondD(2, 3) accept + acceptD(3, interval 1) for all the triples. So, the expected cost equalsD(1, second 2) second + secondD(2, 3) accept + acceptD(3, interval 1 ).


Good question: first, we need to select the expected price for three points without modification. This price comes from the edge. Therefore, we need to calculate the contribution of each edge, dividing by C (3, n) is the expected value.

You can perform tree-based dp preprocessing. Then, each time you modify the edge, you can subtract the expected value from the difference, which is the answer.


/*************************************** * *********************************> File Name: cf_d.cpp> Author: ALex> Mail: 405045132@qq.com> Created Time: wednesday ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # in Clude <iostream> # include <algorithm> using namespace std; const int N = 100010; int num [N]; int par [N]; int head [N], tot; __int64 n; int a [N], B [N], l [N]; double ans; struct node {int weight; int next; int ;} edge [N <1]; void addedge (int from, int to, int weight) {edge [tot]. weight = weight; edge [tot]. to = to; edge [tot]. next = head [from]; head [from] = tot ++;} int dfs (int u, int fa) {int child = 1; par [u] = fa; for (I Nt I = head [u]; ~ I; I = edge [I]. next) {int v = edge [I]. to; if (v = fa) {continue;} int num_v = dfs (v, u); double cnt = (double) (n-num_v) * (num_v-1) * num_v * 1.0/2); cnt + = (double) (n-num_v) * (n-num_v-1) * num_v * 1.0/2); cnt * = 2; ans + = cnt * edge [I]. weight; child + = num_v;} num [u] = child; // printf ("node % d has % d \ n", u, num [u]); return num [u];} int main () {while (~ Scanf ("% I64d", & n) {memset (num, 0, sizeof (num); ans = 0; tot = 0; memset (head,-1, sizeof (head); for (int I = 1; I <= n-1; ++ I) {scanf ("% d ", & a [I], & B [I], & l [I]); addedge (a [I], B [I], l [I]); addedge (B [I], a [I], l [I]);} dfs (1,-1); double cnt = (n-1) * (n-2) * n * 1.0/6; ans/= cnt; int q; int r, w; scanf ("% d", & q ); while (q --) {scanf ("% d", & r, & w); int dis = l [r]-w; l [r] = w; int son; if (par [a [r] = B [r]) {son = a [r];} else {son = B [r];} double x = (double) (n-num [son]) * (num [son]-1) * num [son] * 1.0/2; x + = (double) (n-num [son]-1) * (n-num [son]) * num [son] * 1.0/2; x * = 2; x * = dis; x/= cnt; ans-= x; printf ("%. 10f \ n ", ans) ;}} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.