Problem solution · Go

Codeforces 599E — Sandy and Nuts

Codeforces 599E — Sandy and Nuts: 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
76 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 599E — Sandy and Nuts, 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

  • 76 lines of Go from the credited upstream file 599E.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 599E — Sandy and Nuts · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io"	"math/bits") func cf599E(in io.Reader, out io.Writer) {	var n, m, q, x, y, lca int	Fscan(in, &n, &m, &q)	type tuple struct{ tp, x, y, lca int }	req := make([]tuple, 0, m+q)	for ; m > 0; m-- {		Fscan(in, &x, &y)		req = append(req, tuple{0, x - 1, y - 1, 0})	}	for ; q > 0; q-- {		Fscan(in, &x, &y, &lca)		if x == y && x != lca {			Fprint(out, 0)			return		}		req = append(req, tuple{1, x - 1, y - 1, lca - 1})	} 	check := func(mask, root, subMask, child int) bool {		for _, r := range req {			if mask>>r.x&1 == 0 || mask>>r.y&1 == 0 ||				subMask>>r.x&1 == subMask>>r.y&1 { // 不在考虑范围内 or 已经考虑过了				continue			}			if r.tp == 0 { // 树边				if r.y == root { // 保证 a 上 b 下					r.x, r.y = r.y, r.x				}				if r.x != root || r.y != child {					return false				}			} else if r.lca != root { // a 和 b 在不同的子树/连通块中,所以 rt 必须是 LCA				return false			}		}		return true	} 	f := make([][13]int, 1<<n)	for i := 0; i < n; i++ {		f[1<<i][i] = 1	}	for mask := 2; mask < 1<<n; mask++ {		if mask&(mask-1) == 0 {			continue		}		for t := uint(mask); t > 0; t &= t - 1 {			root := bits.TrailingZeros(t) // mask 子树的根			m := mask ^ 1<<root			lb := m & -m			m ^= lb			for sub, ok := m, true; ok; ok = sub != m {				subMask := sub | lb				for t2 := uint(subMask); t2 > 0; t2 &= t2 - 1 {					child := bits.TrailingZeros(t2) // subMask 子树的根,同时也是 root 的儿子					if check(mask, root, subMask, child) {						f[mask][root] += f[mask^subMask][root] * f[subMask][child]					}				}				sub = (sub - 1) & m			}		}	}	Fprint(out, f[1<<n-1][0])} //func main() { cf599E(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 ↗