← Home
For problem statement at 0-999/800-899/840-849/842/problemC.txt this is a correct solution, but verifier at 0-999/800-899/840-849/842/verifierC.go ends with all tests passed can you fix the verifier? package main

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

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

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

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

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

	a := make([]int, n+1)
	for i := 1; i <= n; i++ {
		a[i] = scanInt()
	}

	adj := make([][]int, n+1)
	for i := 0; i < n-1; i++ {
		u := scanInt()
		v := scanInt()
		adj[u] = append(adj[u], v)
		adj[v] = append(adj[v], u)
	}

	ans := make([]int, n+1)

	var dfs func(u, p, g_all int, S []int)
	dfs = func(u, p, g_all int, S []int) {
		max_ans := g_all
		for _, g := range S {
			if g > max_ans {
				max_ans = g
			}
		}
		ans[u] = max_ans

		for _, v := range adj[u] {
			if v != p {
				next_g_all := gcd(g_all, a[v])
				nextS := make([]int, 0, len(S)+1)
				nextS = append(nextS, g_all)
				for _, g := range S {
					ng := gcd(g, a[v])
					found := false
					for _, x := range nextS {
						if x == ng {
							found = true
							break
						}
					}
					if !found {
						nextS = append(nextS, ng)
					}
				}
				dfs(v, u, next_g_all, nextS)
			}
		}
	}

	dfs(1, 0, a[1], []int{0})

	out := bufio.NewWriter(os.Stdout)
	for i := 1; i <= n; i++ {
		fmt.Fprintf(out, "%d ", ans[i])
	}
	fmt.Fprintln(out)
	out.Flush()
}