Problem solution · Go

Codeforces 1304E — 1-Trees and Queries

Codeforces 1304E — 1-Trees and Queries: 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
100 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 1304E — 1-Trees and Queries, 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

  • 100 lines of Go from the credited upstream file 1304E.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 1304E — 1-Trees and Queries · 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 CF1304E(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush() 	var n, q, v, w, x, y, a, b, k int	Fscan(in, &n)	g := make([][]int, n)	for i := 0; i < n-1; i++ {		Fscan(in, &v, &w)		v--		w--		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				}			}		}	}	stInit(depths)	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)]	}	_d := func(v, w int) int { return dis[v] + dis[w] - dis[lca(v, w)]<<1 } _q:	for Fscan(in, &q); q > 0; q-- {		Fscan(in, &x, &y, &a, &b, &k)		x--		y--		a--		b--		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {			if d <= k && d&1 == k&1 {				Fprintln(out, "YES")				continue _q			}		}		Fprintln(out, "NO")	}} //func main() { CF1304E(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 ↗