cf500d New Year Santa Network

Source: Internet
Author: User

D. New year Santa networktime limit per test2 secondsMemory limit per testMegabytesInputStandard InputOutputStandard Output

New year are coming in Tree world! In this world, as the name implies, there Are  n  cities connected By  n -1 roads, and for any of the 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 intege RS 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 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 citiesC1, c 2, C3 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 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,b i ,   l i   (1≤ a i , b i n ,   a i b i ,  1≤ l i ≤103), denoting That The  I -th Road connects Cities  a i  and  b I , and the length Of  i -th road is  l Sub class= "Lower-index" > 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 integers R J ,   W  Sub class= "Lower-index" > J   (1≤ R J n -1, 1≤ w J ≤103). It means in The  J -th Repair, the length of The  R J -th Road Becomes  W J . It is guaranteed That  w J  is Smaller than the current length of The  R 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).

Test instructions is a random three-point c1,c2,c3 on a tree that calculates dist (C1,C2) +dist (C1,C3) +dist (C2,C3) expectations. And with the edge right modification, each modification output an answer

I started to understand test instructions I was scared silly ... But soon realized that there was probably a conclusion

Then began to scribble casually ... It was found that the three paths were traced, and each side was exactly two times. This theory proves I'm not, but it turns out it's right.

Think again how to count the answers

The shape of the tree is constant, so you can directly figure out how many times each side of the scheme has been passed, and the number will not change.

Consider an edge in the end of the statistics are counted several times: Obviously the path has passed through this side, then the side of the two ends of the two block of Unicom has at least one point. It could be two on one side.

And then the permutations and the combinations.

It's easier to modify, just subtract (the original weight-the modified value) in the answer. * Statistics, this O (1) is done

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include < cmath> #include <set> #include <map> #include <ctime> #include <iomanip> #define LL long long#    Define INF 0x7ffffff#define N 1000010using namespace Std;inline ll read () {LL X=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} LL N,m,cnt;long double ans,todel;struct edge{ll from,to,next,v; LL Rep;} E[4*n]; ll son[n],head[n],dep[n];bool mrk[n];inline void ins (ll u,ll v,ll W) {e[++cnt].to=v;e[cnt].from=u;e[cnt].v=w;e[cnt]. next=head[u];head[u]=cnt;} inline void Insert (LL u,ll v,ll W) {ins (u,v,w); ins (v,u,w);} inline void Dfs (ll x,ll d) {if (mrk[x]) return;mrk[x]=1;son[x]=1;dep[x]=d;for (ll I=head[x];i;i=e[i].next) if (!mrk[e[i]. To] {DFS (e[i].to,d+1); son[x]+=son[e[i].to];}} int main () {n=read (); todel= (long Double) n (n-1) * (n-2)/6.0;for (ll i=1;i<n;i++) {ll x=rEAD (), Y=read (), Z=read (); Insert (x, y, z);} DFS (for ll i=2;i<=cnt;i+=2) {ll now=i/2,x=e[i].from,y=e[i].to;if (Dep[x]>dep[y]) swap (x, y); LL s1=n-son[y],s2=son[y];e[i].rep+= (long Double) s1*s2* (s2-1) +s2*s1* (s1-1); ans+= (long Double) e[i].rep*e[i].v;} M=read (); Cout<<setiosflags (ios::fixed) <<setprecision (ll i=1;i<=m;i++) {ll x=read (), Y=read ( ); LL now=x*2;ans-= (long Double) (e[now].v-y) *e[now].rep;e[now].v=y;cout<<ans/todel<<endl;} return 0;}

cf500d 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.