← Home
For problem statement at 1000-1999/1000-1099/1040-1049/1045/problemC.txt this is a correct solution, but verifier at 1000-1999/1000-1099/1040-1049/1045/verifierC.go ends with all tests passed can you fix the verifier? package main

import (
	"bytes"
	"io"
	"os"
)

func main() {
	buf, _ := io.ReadAll(os.Stdin)
	var pos int
	readInt := func() int {
		for pos < len(buf) && buf[pos] <= ' ' {
			pos++
		}
		if pos >= len(buf) {
			return 0
		}
		res := 0
		for pos < len(buf) && buf[pos] > ' ' {
			res = res*10 + int(buf[pos]-'0')
			pos++
		}
		return res
	}

	n := readInt()
	m := readInt()
	q := readInt()

	head := make([]int, n+1)
	next := make([]int, 2*m+1)
	to := make([]int, 2*m+1)
	edgeCnt := 1

	addEdge := func(u, v int) {
		to[edgeCnt] = v
		next[edgeCnt] = head[u]
		head[u] = edgeCnt
		edgeCnt++
	}

	for i := 0; i < m; i++ {
		u := readInt()
		v := readInt()
		addEdge(u, v)
		addEdge(v, u)
	}

	bctHead := make([]int, 2*n+1)
	bctNext := make([]int, 4*n+1)
	bctTo := make([]int, 4*n+1)
	bctEdgeCnt := 1

	addBCTEdge := func(u, v int) {
		bctTo[bctEdgeCnt] = v
		bctNext[bctEdgeCnt] = bctHead[u]
		bctHead[u] = bctEdgeCnt
		bctEdgeCnt++
	}

	dfn := make([]int, n+1)
	low := make([]int, n+1)
	timer := 0
	stack := make([]int, 0, n)
	numBlocks := n

	var dfs func(int, int)
	dfs = func(u, p int) {
		timer++
		dfn[u] = timer
		low[u] = timer
		stack = append(stack, u)

		for e := head[u]; e != 0; e = next[e] {
			v := to[e]
			if v == p {
				continue
			}
			if dfn[v] > 0 {
				if dfn[v] < low[u] {
					low[u] = dfn[v]
				}
			} else {
				dfs(v, u)
				if low[v] < low[u] {
					low[u] = low[v]
				}
				if low[v] >= dfn[u] {
					numBlocks++
					addBCTEdge(u, numBlocks)
					addBCTEdge(numBlocks, u)
					for {
						w := stack[len(stack)-1]
						stack = stack[:len(stack)-1]
						addBCTEdge(w, numBlocks)
						addBCTEdge(numBlocks, w)
						if w == v {
							break
						}
					}
				}
			}
		}
	}

	dfs(1, 0)

	totalNodes := numBlocks
	up := make([][20]int, totalNodes+1)
	depth := make([]int, totalNodes+1)

	var buildLCA func(int, int, int)
	buildLCA = func(u, p, d int) {
		up[u][0] = p
		depth[u] = d
		for i := 1; i < 20; i++ {
			if up[u][i-1] != 0 {
				up[u][i] = up[up[u][i-1]][i-1]
			}
		}
		for e := bctHead[u]; e != 0; e = bctNext[e] {
			v := bctTo[e]
			if v != p {
				buildLCA(v, u, d+1)
			}
		}
	}

	buildLCA(1, 0, 0)

	getLCA := func(u, v int) int {
		if depth[u] < depth[v] {
			u, v = v, u
		}
		diff := depth[u] - depth[v]
		for i := 0; i < 20; i++ {
			if (diff & (1 << i)) != 0 {
				u = up[u][i]
			}
		}
		if u == v {
			return u
		}
		for i := 19; i >= 0; i-- {
			if up[u][i] != up[v][i] {
				u = up[u][i]
				v = up[v][i]
			}
		}
		return up[u][0]
	}

	var out bytes.Buffer
	out.Grow(q * 10)

	writeInt := func(n int) {
		if n == 0 {
			out.WriteByte('0')
			out.WriteByte('\n')
			return
		}
		var b [20]byte
		i := 19
		for n > 0 {
			b[i] = byte(n%10 + '0')
			n /= 10
			i--
		}
		out.Write(b[i+1:])
		out.WriteByte('\n')
	}

	for i := 0; i < q; i++ {
		u := readInt()
		v := readInt()
		lca := getLCA(u, v)
		ans := (depth[u] + depth[v] - 2*depth[lca]) / 2
		writeInt(ans)
	}
	os.Stdout.Write(out.Bytes())
}