Sebi lives in Chefland where the government is extremely corrupt that usually makes fool out of public by announcing eye catching but non-sustainable schemes. Recently there was a move to increase tourism in the country that was highly lauded. Sebi wants to examine whether the move has some potential or is a hogwash as usual.
The Chefland is a city with very old road infrastructure. The city has N tourist places. All the places are reachable from each other. The corrupt administrators of the city constructed as few roads as possible just ensuring that all the places are reachable from each other, and those too have now gone old with potholes every here and there. Upon this, there is a toll tax for each road too, which you have to pay once for using that road. Once you pay the tax for a road, you can visit it again as many times as possible.
The tourists coming to Chefland usually want to see all the N nice places. They usually have visit in their own vehicle and stay for few days. Also, they are usually not very rich, they want to pay as less toll tax as possible. For promoting tourism, the government offered their citizens a scheme. It was announced that citizens can choose any two places and the government will build a high class road between those two places and that too without any toll tax. Note that citizens may choose to have a high class road between two cities which already have an old road between them.
Sebi is very sceptical of the claims of the announcement. So, he wants to understand the expected toll tax a tourist has to pay to tour the entire city considering that the citizens of Chefland vote for the two cities for constructing high road uniformly randomly. Can you please him in finding this?

-----Input-----
There is a single test case per test file.
The first line of the input contains an integer N denoting the number of tourist spots in Chefland.
Each of the he next N - 1 lines contains three space separated integers u, v, c, denoting that there is a road between tourist spot u and v which has a toll tax of c Rs.

-----Output-----
Output a single line containing the expected toll tax a tourist has to pay for visiting all the N spots after the construction of new road. Your answer will be considered correct if it has an absolute error less than or equal to 1e-2.

-----Constraints-----
- 2 ≤ N ≤ 105
- 1 ≤ u, v ≤ N
- 0 ≤ c ≤ 106

-----Example-----
Input:
3
1 2 3
1 3 2

Output:
2.333333

-----Explanation-----
Assume that the citizens construct the high class road between city 1 and 2. A tourist can visit all the places by just paying a toll tax of 2 Rs.
If the high class road is constructed between city 1 and 3. All the places then can be visited by just paying a toll tax of 3 Rs.
If the cities 2 and 3 are connected by the high class road. All the places can be visited by paying a toll tax of 2Rs. 
Hence expected Rs. that a tourist has to pay in toll tax will be (2 + 3 + 2) / 3 = 7 / 3 = 2.333333

Difficulty: VERY_HARD
Tags: Tree algorithms

Graph algorithms

Data structures

Spanning trees
Skill types: Data structures

Starter code:

Solutions:
n = int(input())
edges = []
for i in range(n - 1):
	(a, b, c) = map(int, input().strip().split())
	edges.append((c, a - 1, b - 1))
edges.sort()
parent = [-1] * n

def find(n):
	if parent[n] < 0:
		return n
	else:
		pt = find(parent[n])
		parent[n] = pt
		return pt
total = 0
num = 0
den = 0
for (c, a, b) in edges:
	a = find(a)
	b = find(b)
	assert a != b
	total += c
	num += parent[a] * parent[b] * c
	den += parent[a] * parent[b]
	if parent[a] > parent[b]:
		parent[b] += parent[a]
		parent[a] = b
	else:
		parent[a] += parent[b]
		parent[b] = a
print('%.11f' % (total - num / den))


def find(n):
	if parent[n] != n:
		parent[n] = find(parent[n])
	return parent[n]

def merge(a, b):
	(a, b) = (find(a), find(b))
	if rank[a] > rank[b]:
		parent[b] = a
		size[a] += size[b]
	else:
		parent[a] = b
		size[b] += size[a]
		if rank[a] == rank[b]:
			rank[b] += 1
n = int(input())
size = {}
rank = {}
parent = {}
edges = []
for i in range(n):
	size[i] = 1
	parent[i] = i
	rank[i] = 1
for i in range(n - 1):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	edges.append([c, a, b])
edges.sort()
S = T = C = 0
for (c, a, b) in edges:
	a = find(a)
	b = find(b)
	S += size[a] * size[b] * c
	T += size[a] * size[b]
	C += c
	merge(a, b)
print(C - S / T)


n = int(input())
size = {}
parent = {}
for i in range(1, n + 1):
	size[i] = 1
	parent[i] = i

def find(a):
	if parent[a] == a:
		return a
	parent[a] = find(parent[a])
	return parent[a]
edges = []
for i in range(n - 1):
	edges.append(list(map(int, input().split())))
edges.sort(key=lambda x: x[2])
c = 0
s = 0
t = 0
for i in range(n - 1):
	a = find(edges[i][0])
	b = find(edges[i][1])
	c += edges[i][2]
	s += size[a] * size[b] * edges[i][2]
	t += size[a] * size[b]
	if size[a] > size[b]:
		size[a] += size[b]
		parent[b] = a
	else:
		size[b] += size[a]
		parent[a] = b
print('%.6f' % (c - float(s) / t))


n = int(input())
edge = []
num = den = sm = 0
size = [1] * (n + 1)
parent = [i for i in range(n + 1)]

def find(x):
	if parent[x] != x:
		parent[x] = find(parent[x])
	return parent[x]
for i in range(n - 1):
	(u, v, c) = list(map(int, input().split()))
	sm += c
	edge.append((c, u, v))
edge.sort()
for (c, u, v) in edge:
	a = find(u)
	b = find(v)
	num += size[a] * size[b] * c
	den += size[a] * size[b]
	if size[a] > size[b]:
		(a, b) = (b, a)
	parent[a] = b
	size[b] += size[a]
print(sm - num / den)


from sys import stdin, stdout
total_cost = 0

def find(a):
	if par[a] == a:
		return a
	else:
		par[a] = find(par[a])
		return par[a]

def union(a, b, c):
	(a, b) = (find(a), find(b))
	nonlocal total_cost
	total_cost += rank[a] * rank[b] * c
	if a != b:
		if rank[a] > rank[b]:
			par[b] = a
			rank[a] += rank[b]
		elif rank[b] > rank[a]:
			par[a] = b
			rank[b] += rank[a]
		else:
			par[a] = b
			rank[b] += rank[a]
n = int(stdin.readline().strip())
par = [i for i in range(n)]
rank = [1 for i in range(n)]
edges = []
for i in range(n - 1):
	(u, v, c) = stdin.readline().strip().split(' ')
	(u, v, c) = (int(u) - 1, int(v) - 1, int(c))
	edges.append((c, u, v))
edges.sort()
tw = 0
for i in edges:
	union(i[1], i[2], i[0])
	tw += i[0]
stdout.write(str(tw - total_cost / (n * (n - 1) / 2)))