Problem solution · Go

Codeforces 832D — Misha, Grisha and Underground

Codeforces 832D — Misha, Grisha and Underground: a Go solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Depth-first search
Source
EndlessCheng Codeforces Go
Length
110 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

Depth-first search

For Codeforces 832D — Misha, Grisha and Underground, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 110 lines of Go from the credited upstream file 832D.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

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 832D — Misha, Grisha and Underground · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"math/bits") // github.com/EndlessCheng/codeforces-gofunc Sol832D(reader io.Reader, writer io.Writer) {	in := bufio.NewScanner(reader)	in.Split(bufio.ScanWords)	out := bufio.NewWriter(writer)	defer out.Flush()	read := func() (x int) {		in.Scan()		for _, b := range in.Bytes() {			x = x*10 + int(b-'0')		}		return	}	maxs := func(vals ...int) int {		ans := vals[0]		for _, val := range vals[1:] {			if val > ans {				ans = val			}		}		return ans	} 	n, q := read(), read()	g := make([][]int, n)	for v := 1; v < n; v++ {		w := read() - 1		g[v] = append(g[v], w)		g[w] = append(g[w], v)	} 	vs := make([]int, 0, 2*n-1)	pos := make([]int, n)	depths := make([]int, 0, 2*n-1)	dis := make([]int, n)	var dfs func(v, fa, d int)	dfs = func(v, fa, d int) {		pos[v] = len(vs)		vs = append(vs, v)		depths = append(depths, d)		dis[v] = d		for _, w := range g[v] {			if w != fa {				dfs(w, v, d+1)				vs = append(vs, v)				depths = append(depths, d)			}		}	}	dfs(0, -1, 0) 	type pair struct{ v, i int }	var st [][18]pair	stInit := func(a []int) {		n := len(a)		st = make([][18]pair, n)		for i := range st {			st[i][0] = pair{a[i], i}		}		for j := uint(1); 1<<j <= n; j++ {			for i := 0; i+(1<<j)-1 < n; i++ {				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]				if st0.v < st1.v {					st[i][j] = st0				} else {					st[i][j] = st1				}			}		}	}	stQuery := func(l, r int) int {		k := uint(bits.Len(uint(r-l+1)) - 1)		st0, st1 := st[l][k], st[r-(1<<k)+1][k]		if st0.v < st1.v {			return st0.i		}		return st1.i	}	lca := func(v, w int) int {		pv, pw := pos[v], pos[w]		if pv > pw {			pv, pw = pw, pv		}		return vs[stQuery(pv, pw)]	}	calcDis := func(v, w int) int {		return dis[v] + dis[w] - 2*dis[lca(v, w)]	} 	stInit(depths)	for ; q > 0; q-- {		a, b, c := read()-1, read()-1, read()-1		v := lca(a, b) ^ lca(a, c) ^ lca(b, c)		Fprintln(out, 1+maxs(calcDis(v, a), calcDis(v, b), calcDis(v, c)))	}} //func main() {//	Sol832D(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 ↗