← Home
For problem statement at 1000-1999/1800-1899/1850-1859/1857/problemG.txt this is a correct solution, but verifier at 1000-1999/1800-1899/1850-1859/1857/verifierG.go ends with case 1 failed: expected 16 got 45

exit status 1 can you fix the verifier? package main

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

type Edge struct {
	u, v, w int
}

func power(base, exp int64) int64 {
	res := int64(1)
	base %= 998244353
	for exp > 0 {
		if exp%2 == 1 {
			res = (res * base) % 998244353
		}
		base = (base * base) % 998244353
		exp /= 2
	}
	return res
}

func find(parent []int, i int) int {
	root := i
	for root != parent[root] {
		root = parent[root]
	}
	curr := i
	for curr != root {
		nxt := parent[curr]
		parent[curr] = root
		curr = nxt
	}
	return root
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buf := make([]byte, 1024*1024*10)
	scanner.Buffer(buf, len(buf))

	nextInt := func() int {
		scanner.Scan()
		res := 0
		for _, b := range scanner.Bytes() {
			res = res*10 + int(b-'0')
		}
		return res
	}

	if !scanner.Scan() {
		return
	}
	t := 0
	for _, b := range scanner.Bytes() {
		t = t*10 + int(b-'0')
	}

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	for i := 0; i < t; i++ {
		n := nextInt()
		S := nextInt()

		edges := make([]Edge, n-1)
		for j := 0; j < n-1; j++ {
			edges[j].u = nextInt()
			edges[j].v = nextInt()
			edges[j].w = nextInt()
		}

		sort.Slice(edges, func(a, b int) bool {
			return edges[a].w < edges[b].w
		})

		parent := make([]int, n+1)
		size := make([]int64, n+1)
		for j := 1; j <= n; j++ {
			parent[j] = j
			size[j] = 1
		}

		ans := int64(1)
		for j := 0; j < n-1; j++ {
			u := edges[j].u
			v := edges[j].v
			w := edges[j].w

			rootU := find(parent, u)
			rootV := find(parent, v)

			if rootU != rootV {
				pairs := size[rootU]*size[rootV] - 1
				ways := int64(S - w + 1)
				ans = (ans * power(ways, pairs)) % 998244353
				parent[rootV] = rootU
				size[rootU] += size[rootV]
			}
		}
		fmt.Fprintln(out, ans)
	}
}