Problem solution · Go

Codeforces 877E — Danil and a Part-time Job

Codeforces 877E — Danil and a Part-time Job: 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
122 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 877E — Danil and a Part-time Job, 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

  • 122 lines of Go from the credited upstream file 877E.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 877E — Danil and a Part-time Job · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"math/bits") // https://space.bilibili.com/206214type seg []struct {	l, r int	ones int	flip bool} func (t seg) maintain(o int) {	t[o].ones = t[o<<1].ones + t[o<<1|1].ones} func (t seg) doFlip(O int) {	o := &t[O]	o.ones = o.r - o.l + 1 - o.ones	o.flip = !o.flip} func (t seg) spread(o int) {	if t[o].flip {		t.doFlip(o << 1)		t.doFlip(o<<1 | 1)		t[o].flip = false	}} func (t seg) build(a []int, o, l, r int) {	t[o].l, t[o].r = l, r	if l == r {		t[o].ones = a[l]		return	}	m := (l + r) >> 1	t.build(a, o<<1, l, m)	t.build(a, o<<1|1, m+1, r)	t.maintain(o)} func (t seg) flip(o, l, r int) {	if l <= t[o].l && t[o].r <= r {		t.doFlip(o)		return	}	t.spread(o)	m := (t[o].l + t[o].r) >> 1	if l <= m {		t.flip(o<<1, l, r)	}	if m < r {		t.flip(o<<1|1, l, r)	}	t.maintain(o)} func (t seg) onesCount(o, l, r int) int {	if l <= t[o].l && t[o].r <= r {		return t[o].ones	}	t.spread(o)	m := (t[o].l + t[o].r) >> 1	if r <= m {		return t.onesCount(o<<1, l, r)	}	if m < l {		return t.onesCount(o<<1|1, l, r)	}	return t.onesCount(o<<1, l, r) + t.onesCount(o<<1|1, l, r)} func cf877E(in io.Reader, _w io.Writer) {	out := bufio.NewWriter(_w)	defer out.Flush()	var n, p, time, q int	var op string	Fscan(in, &n)	g := make([][]int, n)	for w := 1; w < n; w++ {		Fscan(in, &p)		g[p-1] = append(g[p-1], w)	}	a := make([]int, n)	for i := range a {		Fscan(in, &a[i])	} 	b := make([]int, n)	nodes := make([]struct{ l, r int }, n)	var dfs func(int)	dfs = func(v int) {		nodes[v].l = time		b[time] = a[v]		time++		for _, w := range g[v] {			dfs(w)		}		nodes[v].r = time - 1	}	dfs(0) 	t := make(seg, 2<<bits.Len(uint(n-1)))	t.build(b, 1, 0, n-1)	for Fscan(in, &q); q > 0; q-- {		Fscan(in, &op, &p)		o := nodes[p-1]		if op[0] == 'p' {			t.flip(1, o.l, o.r)		} else {			Fprintln(out, t.onesCount(1, o.l, o.r))		}	}} //func main() { cf877E(bufio.NewReader(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 ↗