Good Bye D. New year Santa Network

Source: Internet
Author: User

D. New year Santa networktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard o Utput

New year was coming in Tree world! In this world, as the name implies, there isN cities connected by n?-? 1 roads Distinct cities there always exists a path between them. The cities is numbered by integers from 1 toN, and the roads is numbered by integers from1 to n?-? 1. Let ' s defineD(u,? V) as total length of roads in the path between Cityu 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 is going to Become w i , which is shorter than its length before. Assume the current year is Year1 .

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< Em>c 1 , C 2 , C 3 and make exactly one Warehouse in all 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 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 from1 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 an integer n (3?≤? N? ≤?105)-the number of cities in the Tree world.

Next n?-? 1Lines describe the roads. TheI-th Line of them (1?≤? i? ≤? n?-? 1) contains three space-separated integers ai , bi , li (1?≤? a i,? b i? ≤? n , ai? ≠? bi ,1?≤? L i? ≤?103 ), denoting that theI-th Road connects Cities ai and bi , and the length ofI-th Road is li .

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?≤? j. ≤? Q ) contains space-separated integers RJ , WJ (1?≤? R J. ≤? n?-? 1 ,1?≤? W J. ≤?103 ). It means in theJ-th repair, the length of the RJ -th Road becomes WJ . It's Guaranteed that 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 ' texceed?-? 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 is 6 triples: (1,?2,?3),? ( 1,?3,?2),? (2,?1,?3),? (2,?3,?1),? (3,?1,?2),? (3,?2,?1). Becausen? =?3, the cost needed to build the network are alwaysD(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).


You can know, if it is a complete picture, for each side, will pass it n-2 times, so, in the original image, Dfs out of each side after the cut, divided into two parts each have how many points, assuming that each has a a and a, then this edge will be passed

AB (n-2) times


#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip>using namespace std; struct Edge{int to,id; Edge (int to,int ID) {this->to=to;this->id=id;}}; Vector<edge>edge[100010];int cnt[100010];int dfs (int pre,int from) {int sum=0;for (int i=0;i<edge[from].size ( ) {i++) {int to=edge[from][i].to,id=edge[from][i].id;if (to==pre) Continue;sum+=cnt[id]=dfs (from,to);} return sum+1;} struct Road{int a,b,w;friend IStream & operator >> (IStream &is,road &x) {is>>x.a>>x.b> >x.w;return is;}} Road[100010];int Main () {int n;cin>>n;for (int i=1;i<n;i++) {Cin>>road[i];edge[road[i].a].push_back ( Edge (Road[i].b,i)); Edge[road[i].b].push_back (Edge (Road[i].a,i));} DFS (0,1);d ouble cn3=double (N) * (n-1) * (n-2)/6,ANS=0;FOR (int i=1;i<=n;i++) ans+= (double) cnt[i]* (N-cnt[i]) * (n-2) *road[i].w;ans/=cn3;int q;cin>>q;while (q--) {int id,w;cin>>id>>w;ans+= (Double) cnt[id]* (N-cnt[id]) * (n-2) * (W-ROAD[ID].W)/cn3;road[id].w=w;cout< <fixed<<setprecision (9) <<ans<<endl;}}


Good Bye D. New year Santa Network

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.