Problem solution · Go

Codeforces 628F — Bear and Fair Set

Codeforces 628F — Bear and Fair Set: a Go solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Breadth-first search
Source
EndlessCheng Codeforces Go
Length
103 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

Breadth-first search

For Codeforces 628F — Bear and Fair Set, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 103 lines of Go from the credited upstream file 628F.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

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 628F — Bear and Fair Set · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io"	"math"	"slices") // https://github.com/EndlessChengfunc cf628F(in io.Reader, out io.Writer) {	var n, maxV, q int	Fscan(in, &n, &maxV, &q)	q += 2	type pair struct{ r, num int }	a := make([]pair, q)	for i := 1; i < q-1; i++ {		Fscan(in, &a[i].r, &a[i].num)	}	a[q-1] = pair{maxV, n}	slices.SortFunc(a, func(a, b pair) int { return a.r - b.r }) 	const k = 5	st := q + k	end := st + 1	type nb struct{ to, rid, cap int }	g := make([][]nb, end+1)	addEdge := func(from, to, cap int) {		g[from] = append(g[from], nb{to, len(g[to]), cap})		g[to] = append(g[to], nb{from, len(g[from]) - 1, 0})	} 	for i := 1; i < q; i++ {		num := a[i].num - a[i-1].num		l, r := a[i-1].r, a[i].r		if num < 0 || num > r-l {			Fprint(out, "unfair")			return		}		addEdge(st, i, num)		for j := range k {			addEdge(i, j+q, (r+k-j)/k-(l+k-j)/k)		}	}	for j := range k {		addEdge(j+q, end, n/k)	} 	dis := make([]int, len(g))	bfs := func() bool {		clear(dis)		dis[st] = 1		q := []int{st}		for len(q) > 0 {			v := q[0]			q = q[1:]			for _, e := range g[v] {				if w := e.to; e.cap > 0 && dis[w] == 0 {					dis[w] = dis[v] + 1					q = append(q, w)				}			}		}		return dis[end] > 0	}	iter := make([]int, len(g))	var dfs func(int, int) int	dfs = func(v, totalFlow int) (curFlow int) {		if v == end {			return totalFlow		}		for ; iter[v] < len(g[v]); iter[v]++ {			e := &g[v][iter[v]]			if w := e.to; e.cap > 0 && dis[w] > dis[v] {				f := dfs(w, min(totalFlow-curFlow, e.cap))				if f == 0 {					continue				}				e.cap -= f				g[w][e.rid].cap += f				curFlow += f				if curFlow == totalFlow {					break				}			}		}		return	}	maxFlow := 0	for bfs() {		clear(iter)		maxFlow += dfs(st, math.MaxInt)	} 	if maxFlow == n {		Fprint(out, "fair")	} else {		Fprint(out, "unfair")	}} //func main() { cf628F(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 ↗