Problem solution · Go

Codeforces 740D

Codeforces 740D: 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
88 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 740D, 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

  • 88 lines of Go from the credited upstream file 740D.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 740D · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // TODO https://www.luogu.com.cn/blog/qiyue7ACM/solution-cf739b// 也可以用一个全局栈记录当前 DFS 链,然后在链上二分。这样做更简单但是适用范围较窄 // github.com/EndlessCheng/codeforces-gofunc CF740D(_r io.Reader, _w io.Writer) {	in := bufio.NewScanner(_r)	in.Split(bufio.ScanWords)	out := bufio.NewWriter(_w)	defer out.Flush()	read := func() (x int) {		in.Scan()		for _, b := range in.Bytes() {			x = x*10 + int(b-'0')		}		return	}	type edge struct{ to, l int } 	n := read()	a := make([]int64, n)	for i := range a {		a[i] = int64(read())	}	const mx = 19	pa := make([][mx]int, n)	pa[0][0] = -1	g := make([][]edge, n)	for w := 1; w < n; w++ {		v := read() - 1		g[v] = append(g[v], edge{w, read()})		pa[w][0] = v	}	for k := 0; k+1 < mx; k++ {		for v := range pa {			if p := pa[v][k]; p != -1 {				pa[v][k+1] = pa[p][k]			} else {				pa[v][k+1] = -1			}		}	} 	dep := make([]int64, n)	var f func(int, int64)	f = func(v int, d int64) {		dep[v] = d		for _, e := range g[v] {			f(e.to, d+int64(e.l))		}	}	f(0, 0) 	diff := make([]int, n)	for v, val := range a {		u := v		dv := dep[v]		for i := mx - 1; i >= 0; i-- {			if p := pa[u][i]; p != -1 && dv-dep[p] <= val {				u = p			}		}		diff[v]++		diff[u]--	} 	ans := make([]interface{}, n)	var f2 func(int) int	f2 = func(v int) (sum int) {		for _, e := range g[v] {			sum += f2(e.to)		}		ans[v] = sum		return sum + diff[v]	}	f2(0)	Fprint(out, ans...)} //func main() { CF740D(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 ↗