← Home
For problem statement at 0-999/500-599/500-509/500/problemD.txt this is a correct solution, but verifier at 0-999/500-599/500-509/500/verifierD.go ends with case 1 failed: expected 45.500000000000000
45.500000000000000 got 45.5000000000
45.5000000000
exit status 1 can you fix the verifier? ```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscan(reader, &n)

	// For each edge, we need to compute how many pairs (u,v) have that edge on their path.
	// For an edge that splits the tree into two components of size s and (n-s),
	// the number of pairs is s * (n - s).
	// 
	// The expected cost = E[d(c1,c2) + d(c2,c3) + d(c3,c1)]
	// For a random triple of distinct cities, by linearity of expectation and symmetry:
	// E[cost] = 3 * E[d(ci, cj)] for a random pair among the triple.
	//
	// Actually, let's think more carefully.
	// We choose 3 distinct cities c1, c2, c3 uniformly at random (ordered triples).
	// E[d(c1,c2) + d(c2,c3) + d(c3,c1)] = 3 * E[d(c1,c2)]
	// where c1, c2 are two distinct cities chosen from the triple.
	//
	// For ordered triples, E[d(c1,c2)] = sum over all ordered pairs (u,v) with u!=v of d(u,v) * (n-2) / (n*(n-1)*(n-2))
	// Wait, let me reconsider.
	//
	// Total ordered triples = n*(n-1)*(n-2)
	// E[cost] = (1 / (n*(n-1)*(n-2))) * sum over all ordered triples of (d(c1,c2)+d(c2,c3)+d(c3,c1))
	//
	// By symmetry among pairs, sum of d(c1,c2) over all ordered triples = 
	// for each ordered pair (c1,c2) with c1!=c2, d(c1,c2) * (n-2) [choices for c3]
	// = (n-2) * sum_{u!=v} d(u,v)
	//
	// Similarly for d(c2,c3) and d(c3,c1).
	// So total = 3 * (n-2) * sum_{u!=v} d(u,v)
	// E[cost] = 3*(n-2)*S / (n*(n-1)*(n-2)) = 3*S / (n*(n-1))
	// where S = sum over all ordered pairs (u,v), u!=v, of d(u,v) = 2 * sum over unordered pairs of d(u,v)
	//
	// So E[cost] = 6 * (sum over unordered pairs of d(u,v)) / (n*(n-1))
	//
	// For each edge with weight w splitting tree into sizes s and (n-s),
	// its contribution to sum of unordered pair distances = w * s * (n-s)
	//
	// So E[cost] = 6 * sum_e(w_e * s_e * (n - s_e)) / (n*(n-1))

	type Edge struct {
		a, b, w int
	}

	edges := make([]Edge, n-1)
	adj := make([][]struct{ to, idx int }, n+1)

	for i := 0; i < n-1; i++ {
		fmt.Fscan(reader, &edges[i].a, &edges[i].b, &edges[i].w)
		a, b := edges[i].a, edges[i].b
		adj[a] = append(adj[a], struct{ to, idx int }{b, i})
		adj[b] = append(adj[b], struct{ to, idx int }{a, i})
	}

	// Compute subtree sizes via BFS from node 1
	subtreeSize := make([]int, n+1)
	edgeSubSize := make([]int, n-1) // for edge i, the size of the subtree on one side
	parent := make([]int, n+1)
	order := make([]int, 0, n)
	visited := make([]bool, n+1)
	visited[1] = true
	order = append(order, 1)
	for front := 0; front < len(order); front++ {
		u := order[front]
		for _, e := range adj[u] {
			if !visited[e.to] {
				visited[e.to] = true
				parent[e.to] = u
				order = append(order, e.to)
			}
		}
	}
	for i := 0; i < n; i++ {
		subtreeSize[order[i]] = 1
	}
	for i := n - 1; i >= 1; i-- {
		u := order[i]
		subtreeSize[parent[u]] += subtreeSize[u]
	}
	// For each edge, figure out which endpoint is the child
	for i := 0; i < n-1; i++ {
		a, b := edges[i].a, edges[i].b
		if parent[b] == a {
			edgeSubSize[i] = subtreeSize[b]
		} else {
			edgeSubSize[i] = subtreeSize[a]
		}
	}

	// Compute initial total sum
	totalSum := int64(0)
	for i := 0; i < n-1; i++ {
		s := int64(edgeSubSize[i])
		totalSum += int64(edges[i].w) * s * (int64(n) - s)
	}

	denom := float64(int64(n) * int64(n-1))

	var q int
	fmt.Fscan(reader, &q)
	for ; q > 0; q-- {
		var r, w int
		fmt.Fscan(reader, &r, &w)
		r-- // 0-indexed
		s := int64(edgeSubSize[r])
		contrib := s * (int64(n) - s)
		totalSum += (int64(w) - int64(edges[r].w)) * contrib
		edges[r].w = w
		expected := 6.0 * float64(totalSum) / denom
		fmt.Fprintf(writer, "%.10f\n", expected)
	}
}
```