Problem solution · Go

Codeforces 1290C — Prefix Enlightenment

Codeforces 1290C — Prefix Enlightenment: 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
67 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 1290C — Prefix Enlightenment, 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

  • 67 lines of Go from the credited upstream file 1290C.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 1290C — Prefix Enlightenment · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gofunc cf1290C(in io.Reader, _w io.Writer) {	out := bufio.NewWriter(_w)	defer out.Flush()	var n, k, m, v, ans int	var s string	Fscan(in, &n, &k, &s)	idx := make([][]int, n)	fa := make([]int, k+1)	sz := make([][2]int, k+1)	sz[0][0] = 1e9 // 强制 x_0=1 的成本为无穷大	for i := 1; i <= k; i++ {		Fscan(in, &m)		for range m {			Fscan(in, &v)			idx[v-1] = append(idx[v-1], i)		}		fa[i] = i		// sz[i][0/1] 表示集合中的与 i 开关状态相同/不同的元素个数		sz[i][0] = 1	} 	dis := make([]byte, k+1)	var find func(int) int	find = func(x int) int {		if fa[x] != x {			f := find(fa[x])			dis[x] ^= dis[fa[x]]			fa[x] = f		}		return fa[x]	}	minOp := func(x int) int { return min(sz[x][0], sz[x][1]) } 	for i, p := range idx {		if p == nil {			Fprintln(out, ans)			continue		}		from, to := p[0], 0		if len(p) > 1 {			to = p[1]		}		x, y := find(from), find(to)		if x != y {			d := s[i]&1 ^ 1 ^ dis[from] ^ dis[to]			dis[x] = d			fa[x] = y			ans -= minOp(x) + minOp(y)			sz[y][0] += sz[x][d]			sz[y][1] += sz[x][d^1]			ans += minOp(y)		}		Fprintln(out, ans)	}} //func main() { cf1290C(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 ↗