Problem solution · Go

Codeforces 1051F — The Shortest Statement

Codeforces 1051F — The Shortest Statement: a Go solution using stack-based processing. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Stack-based processing
Source
EndlessCheng Codeforces Go
Length
140 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Stack-based processing

For Codeforces 1051F — The Shortest Statement, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.

  1. Define what every stack entry represents.
  2. Pop entries once the current item resolves or invalidates them.
  3. Push the current item with only the information later steps need.

Code notes

  • 140 lines of Go from the credited upstream file 1051F.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

If each item is pushed and popped at most once, the stack work is linear.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 1051F — The Shortest Statement · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	"container/heap"	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gotype vd51 struct {	v   int	dis int64}type hp51 []vd51 func (h hp51) Len() int              { return len(h) }func (h hp51) Less(i, j int) bool    { return h[i].dis < h[j].dis }func (h hp51) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }func (h *hp51) Push(v interface{})   { *h = append(*h, v.(vd51)) }func (h *hp51) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }func (h *hp51) push(v vd51)          { heap.Push(h, v) }func (h *hp51) pop() vd51            { return heap.Pop(h).(vd51) } func CF1051F(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush()	min := func(a, b int64) int64 {		if a > b {			return b		}		return a	} 	var n, m, q, v, w, wt int	Fscan(in, &n, &m)	type nb struct{ to, wt int }	g := make([][]nb, n)	for i := 0; i < m; i++ {		Fscan(in, &v, &w, &wt)		v--		w--		g[v] = append(g[v], nb{w, wt})		g[w] = append(g[w], nb{v, wt})	} 	spV := map[int]bool{}	const mx = 17	pa := make([][mx]int, n)	dep := make([]int, n)	dep2 := make([]int64, n)	var f func(int, int)	f = func(v, fa int) {		pa[v][0] = fa		for _, e := range g[v] {			w := e.to			if dep[w] == 0 {				dep[w] = dep[v] + 1				dep2[w] = dep2[v] + int64(e.wt)				f(w, v)			} else if w != fa {				spV[v] = true				spV[w] = true			}		}	}	f(0, -1) 	spDis := make([][]int64, 0, len(spV))	for v := range spV {		dis := make([]int64, n)		for i := range dis {			dis[i] = 1e18		}		dis[v] = 0		h := hp51{{v, 0}}		for len(h) > 0 {			vd := h.pop()			v := vd.v			if dis[v] < vd.dis {				continue			}			for _, e := range g[v] {				w := e.to				if newD := dis[v] + int64(e.wt); newD < dis[w] {					dis[w] = newD					h.push(vd51{w, newD})				}			}		}		spDis = append(spDis, dis)	} 	for i := 0; i+1 < mx; i++ {		for v := range pa {			if p := pa[v][i]; p != -1 {				pa[v][i+1] = pa[p][i]			} else {				pa[v][i+1] = -1			}		}	}	uptoDep := func(v, d int) int {		for i := 0; i < mx; i++ {			if (dep[v]-d)>>i&1 > 0 {				v = pa[v][i]			}		}		return v	}	_lca := func(v, w int) int {		if dep[v] > dep[w] {			v, w = w, v		}		w = uptoDep(w, dep[v])		if w == v {			return v		}		for i := mx - 1; i >= 0; i-- {			if pv, pw := pa[v][i], pa[w][i]; pv != pw {				v, w = pv, pw			}		}		return pa[v][0]	}	for Fscan(in, &q); q > 0; q-- {		Fscan(in, &v, &w)		v--		w--		d := dep2[v] + dep2[w] - dep2[_lca(v, w)]*2		for _, dis := range spDis {			d = min(d, dis[v]+dis[w])		}		Fprintln(out, d)	}} //func main() { CF1051F(os.Stdin, os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗