New Year is coming in Tree World! In this world, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to n, and the roads are numbered by integers 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 in Tree World repairs exactly one road per year. As a result, the length of one road decreases. It is already known that 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 that the current year is year 1.

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 cities c_1, c_2, c_3 and make exactly one warehouse in each city. The k-th (1 ≤ k ≤ 3) Santa will 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 an 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 are 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 an integer n (3 ≤ n ≤ 10^5) — 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 a_{i}, b_{i}, l_{i} (1 ≤ a_{i}, b_{i} ≤ n, 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 ≤ 10^5) — the number of road length changes.

Next q lines describe the length changes. The j-th line of them (1 ≤ j ≤ q) contains two space-separated integers r_{j}, w_{j} (1 ≤ r_{j} ≤ n - 1, 1 ≤ w_{j} ≤ 10^3). It means that 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 Tree World. The answer will be considered correct if its absolute and relative error doesn't exceed 10^{ - 6}.


-----Examples-----
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 are 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 is 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).

Difficulty: HARD
Tags: Tree algorithms

Graph algorithms

Graph traversal

Combinatorics

Starter code:

Solutions:
import sys
sys.setrecursionlimit(1500)
MAX = 100005
g = [[] for _ in range(MAX)]
vis = [False] * MAX
dp = [0] * MAX
prod = [0] * MAX
edges = []
order = []

def dfs(st):
	stack = []
	stack.append((st, -1))
	vis[st] = True
	while stack:
		(st, parent) = stack.pop()
		order.append(st)
		vis[st] = True
		if st == parent:
			continue
		for i in g[st]:
			if vis[i[0]]:
				continue
			stack.append((i[0], st))
n = int(input())
for i in range(n - 1):
	(a, b, w) = list(map(int, sys.stdin.readline().split(' ')))
	g[a].append([b, w])
	g[b].append([a, w])
	edges.append([[a, b], w])
dfs(1)
order.reverse()
for st in order:
	dp[st] = 1
	for i in g[st]:
		dp[st] += dp[i[0]]
tot = 0
curr = 1
div = n * (n - 1) / 2
for i in edges:
	a = i[0][0]
	b = i[0][1]
	sa = dp[a]
	sb = dp[b]
	tot += min(sa, sb) * (n - min(sa, sb)) * i[1]
	prod[curr] = min(sa, sb) * (n - min(sa, sb))
	curr += 1
q = int(input())
for i in range(q):
	(q1, q2) = list(map(int, sys.stdin.readline().split(' ')))
	tot -= prod[q1] * edges[q1 - 1][1]
	edges[q1 - 1][1] = q2
	tot += prod[q1] * edges[q1 - 1][1]
	sys.stdout.write(str(tot * 3 / div) + '\n')


import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.setrecursionlimit(10 ** 5)

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

@bootstrap
def dfs(u, p):
	global ans
	for j in adj[u]:
		if j != p:
			yield dfs(j, u)
			sub[u] += sub[j]
	p1 = sub[u]
	p2 = n - sub[u]
	d1[u, p] = p1 * p2 * (p1 + p2 - 2)
	d1[p, u] = p1 * p2 * (p1 + p2 - 2)
	ans += p1 * p2 * (p1 + p2 - 2) * d[u, p]
	yield
n = int(input())
adj = [[] for i in range(n + 1)]
edges = []
d = defaultdict(lambda : 0)
d1 = defaultdict(lambda : 0)
ans = 0
for i in range(n - 1):
	(u, v, l) = map(int, input().split())
	d[u, v] = l
	d[v, u] = l
	edges.append([u, v])
	adj[u].append(v)
	adj[v].append(u)
sub = [1 for i in range(n + 1)]
dfs(1, 0)
val = n * (n - 1) * (n - 2)
for q in range(int(input())):
	(nu, new) = map(int, input().split())
	edge = edges[nu - 1]
	(u, v) = (edge[0], edge[1])
	old = d[u, v]
	tot = d1[u, v]
	ans += tot * (new - old)
	d[u, v] = new
	d[v, u] = new
	print(6 * ans / val)


from queue import Queue
import sys
cost = []

def readarray():
	return map(int, input().split(' '))
n = int(input())
graph = [[] for i in range(n)]
for i in range(n - 1):
	(u, v, c) = readarray()
	(u, v) = (u - 1, v - 1)
	cost.append(c)
	graph[u].append((v, i))
	graph[v].append((u, i))
order = []
used = [0] * n
q = [0] * (n + n)
qh = qt = 0
used[qh] = 1
qh += 1
while qt < qh:
	v = q[qt]
	qt += 1
	order.append(v)
	for (to, e) in graph[v]:
		if used[to]:
			continue
		used[to] = 1
		q[qh] = to
		qh += 1
order.reverse()
sz = [0 for x in range(n)]
for v in order:
	sz[v] = 1
	for (to, e) in graph[v]:
		sz[v] += sz[to]
distanceSum = 0.0
edgeMult = [0] * n
for v in range(n):
	for (to, e) in graph[v]:
		x = min(sz[v], sz[to])
		edgeMult[e] = x
		distanceSum += 1.0 * cost[e] * x * (n - x)
distanceSum /= 2.0
queryCnt = int(input())
ans = []
for i in range(queryCnt):
	(x, y) = readarray()
	x -= 1
	distanceSum -= 1.0 * cost[x] * edgeMult[x] * (n - edgeMult[x])
	cost[x] = y
	distanceSum += 1.0 * cost[x] * edgeMult[x] * (n - edgeMult[x])
	ans.append('%.10lf' % (distanceSum / n / (n - 1) * 6.0))
print('\n'.join(ans))


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa = ifa[::-1]

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class SMT:

	def __init__(self, arr):
		self.n = len(arr) - 1
		self.arr = [0] * (self.n << 2)
		self.lazy = [0] * (self.n << 2)

		def Build(l, r, rt):
			if l == r:
				self.arr[rt] = arr[l]
				return
			m = l + r >> 1
			Build(l, m, rt << 1)
			Build(m + 1, r, rt << 1 | 1)
			self.pushup(rt)
		Build(1, self.n, 1)

	def pushup(self, rt):
		self.arr[rt] = self.arr[rt << 1] + self.arr[rt << 1 | 1]

	def pushdown(self, rt, ln, rn):
		if self.lazy[rt]:
			self.lazy[rt << 1] += self.lazy[rt]
			self.lazy[rt << 1 | 1] = self.lazy[rt]
			self.arr[rt << 1] += self.lazy[rt] * ln
			self.arr[rt << 1 | 1] += self.lazy[rt] * rn
			self.lazy[rt] = 0

	def update(self, L, R, c, l=1, r=None, rt=1):
		if r == None:
			r = self.n
		if L <= l and r <= R:
			self.arr[rt] += c * (r - l + 1)
			self.lazy[rt] += c
			return
		m = l + r >> 1
		self.pushdown(rt, m - l + 1, r - m)
		if L <= m:
			self.update(L, R, c, l, m, rt << 1)
		if R > m:
			self.update(L, R, c, m + 1, r, rt << 1 | 1)
		self.pushup(rt)

	def query(self, L, R, l=1, r=None, rt=1):
		if r == None:
			r = self.n
		if L <= l and R >= r:
			return self.arr[rt]
		m = l + r >> 1
		self.pushdown(rt, m - l + 1, r - m)
		ans = 0
		if L <= m:
			ans += self.query(L, R, l, m, rt << 1)
		if R > m:
			ans += self.query(L, R, m + 1, r, rt << 1 | 1)
		return ans

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def GP(it):
	return [(ch, len(list(g))) for (ch, g) in groupby(it)]

class DLN:

	def __init__(self, val):
		self.val = val
		self.pre = None
		self.next = None

@bootstrap
def dfs(r, p):
	if len(g[r]) == 1 and p != -1:
		yield 1
	res = 1
	for child in g[r]:
		if child != p:
			tmp = (yield dfs(child, r))
			cnt[d[tuple(sorted([r, child]))]] = tmp * (n - tmp)
			res += tmp
	yield res
t = 1
for i in range(t):
	n = N()
	edg = []
	d = {}
	cnt = [0] * (n - 1)
	g = [[] for i in range(n)]
	for i in range(n - 1):
		(a, b, l) = RL()
		a -= 1
		b -= 1
		g[a].append(b)
		g[b].append(a)
		if a > b:
			(a, b) = (b, a)
		d[a, b] = i
		edg.append(l)
	dfs(0, -1)
	ans = sum((edg[i] * cnt[i] for i in range(n - 1)))
	ans = ans * 6 / (n * (n - 1))
	q = N()
	for i in range(q):
		(c, l) = RL()
		dec = edg[c - 1] - l
		edg[c - 1] = l
		ans -= dec * cnt[c - 1] * 6 / ((n - 1) * n)
		print(ans)