Problem solution · Go

Codeforces 29D — Ant on the Tree

Codeforces 29D — Ant on the Tree: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
85 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

Direct simulation

For Codeforces 29D — Ant on the Tree, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 85 lines of Go from the credited upstream file 29D.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

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 29D — Ant on the Tree · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // 另一种写法是从 1 走到 l[0], 从 l[0] 走到 l[1]...// 统计经过的点,若长度超过 2*n-1 则不合法// 计算每个节点的父亲,以及 l 中相邻两叶子的 LCA,利用这些数据来计算路径// 采用 Tarjan 的离线写法可以做到 O(n) 的优秀复杂度 // github.com/EndlessCheng/codeforces-gofunc CF29D(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush() 	var n, v, w int	Fscan(in, &n)	g := make([][]int, n+1)	g[1] = append(g[1], 0)	for i := 1; i < n; i++ {		Fscan(in, &v, &w)		g[v] = append(g[v], w)		g[w] = append(g[w], v)	}	l := []int{} 	ms := make([]map[int]bool, n+1)	var f func(v, fa int) map[int]bool	f = func(v, fa int) map[int]bool {		ms[v] = map[int]bool{}		if len(g[v]) == 1 {			ms[v][v] = true			Fscan(in, &w)			l = append(l, w)		}		for _, w := range g[v] {			if w != fa {				for u := range f(w, v) {					ms[v][u] = true				}			}		}		return ms[v]	}	f(1, 0) 	vs := []int{}	var f2 func(int, int, []int) bool	f2 = func(v, fa int, l []int) bool {		if len(g[v]) == 1 {			vs = append(vs, v)			return l[0] == v		}	o:		for len(l) > 0 {			for _, w := range g[v] {				if w != fa && ms[w][l[0]] {					vs = append(vs, v)					if !f2(w, v, l[:len(ms[w])]) {						return false					}					l = l[len(ms[w]):]					continue o				}			}			break		}		vs = append(vs, v)		return len(l) == 0	}	if f2(1, 0, l) {		for _, v := range vs {			Fprint(out, v, " ")		}		return	}	Fprint(out, -1)} //func main() { CF29D(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 ↗