Codeforces 500D New year Santa Network (tree + count)

Source: Internet
Author: User

D. New year Santa Network

Time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

New year was coming in Tree world! In this world, as the name implies, there is N cities connected by N -1 roads Distinct cities there always exists a path between them. The cities is numbered by integers from 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 on 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  R I -th Road was going to Become  W I , which is shorter than its length before. Assume the current year is 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, C 2, C3 and make exactly one warehouse in each city. The k-th (1≤ k ≤ 3) Santa would take charge of the warehouse in city C k.

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 of needed to build this network equals to D ( C 1, C 2) + D ( C 2, C 3) + d ( C 3, C 1)  dollars. Santas is too busy to find the best place, so they decided to Choose  C 1, C 2, C 3 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.

Input

The first line contains a integer n (3≤ n ≤105)-the number of cities in Tree WORLD.

NextN-1 lines describe the roads. TheI-th Line of them (1≤IN-1) contains three space-separated integersaI,bI, l i   (1≤ A i , b i n ,  < Span class= "Tex-span" > a i b I ,  1≤ l i ≤10 3), denoting that The  I -th Road connects cities  a i  and  b I , and the length Of  i -th Road Is  l i .

The next line contains an integer q (1≤ q ≤105)-the number of road length changes.

NextQLines describe the length changes. TheJ-th Line of them (1≤JQ) contains space-separated integersR J, wJ (1≤ RJn -1, 1≤ w c13>J ≤103). It means in the J-th Repair, the length of the RJ-th Road becomes W J. It is guaranteed this wJ is smaller than the current length of the R C33>j-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.

Sample Test (s) input
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
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 of needed to build the network are 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).

It's a pity that I didn't make it in this game (or yellow). Test instructions is given a tree with weights.

Ask you to choose any of the 3 points in the tree C1, C2, C3 D (C1,C2) + D (C2,C3) + D (C1, C3) expectations.

Because the sides of each tree are bridges.

As long as each edge is calculated in all cases [C (3, N)] The number of uses CNT can know the total distance d = Sigma (Cnt[i] *w[i])

Then exp = d/c (3, N).

Q Ask to change the edge is to subtract the previous side expectations, plus the new Benquan to seek out the expectations.

Note to use, Cnt[i]*w[i]/C (3, N) to handle the good side, that is, the side expected.

If you first find out the total distance and then C (3, N) then the data overflow (hanging Big Data ah Qaq).

#include <bits/stdc++.h>using namespaceStd;typedef pair<int,int>PII;#defineX First#defineY SecondConst intN =100010;intN, Cnt[n];DoubleW[n], Cn3, Res;vector<pii>G[n];intDfsintU,intFA) {    inttot =0 ;  for(inti =0; I < g[u].size (); ++i) {        intv = g[u][i]. X, id =G[u][i].        Y if(v = = FA)Continue ; Cnt[id]=Dfs (v, u); Tot+=Cnt[id]; }    returnTot +1 ;}DoubleCalintID) {    DoubleA = Cnt[id], B = N-Cnt[id]; return 2.0*w[id]/cn3* (A * (a)1)/2.0*b + b* (b-1)/2.0*a);}intMain () {intu, v; CIN>>N; Cn3=1.0*n* (n1) * (n2)/6, res =0 ;  for(inti =1; 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)); }    intm = DFS (1,0);  for(inti =1; I < n; ++i) Res + =cal (i); intQ; scanf"%d",&q);  while(q--){        intx;Doubley; scanf"%D%LF",&x,&y); Res-= cal (x); W[x] = y; res + =cal (x); printf ("%.9lf\n", RES); }}
View Code

Codeforces 500D New year Santa Network (tree + count)

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.