Problem solution · Go

Codeforces 1788F — XOR, Tree, and Queries

Codeforces 1788F — XOR, Tree, and Queries: 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
85 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 1788F — XOR, Tree, and Queries, 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

  • 85 lines of Go from the credited upstream file 1788F.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 1788F — XOR, Tree, and Queries · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // https://space.bilibili.com/206214func CF1788F(in io.Reader, _w io.Writer) {	out := bufio.NewWriter(_w)	defer out.Flush()	var n, q, v, w, d int	Fscan(in, &n, &q)	es := make([]struct{ x, y int }, n-1)	deg := make([]byte, n)	for i := range es {		Fscan(in, &es[i].x, &es[i].y)		es[i].x--		es[i].y--		deg[es[i].x] ^= 1		deg[es[i].y] ^= 1	} 	fa := make([]int, n)	for i := range fa {		fa[i] = i	}	dis := make([]int, n)	var find func(int) int	find = func(x int) int {		if fa[x] != x {			rt := find(fa[x])			dis[x] ^= dis[fa[x]]			fa[x] = rt		}		return fa[x]	}	merge := func(from, to, d int) bool {		x, y := find(from), find(to)		if x == y {			return dis[to]^dis[from] == d		}		dis[x] = d ^ dis[to] ^ dis[from]		fa[x] = y		return true	} 	for range q {		Fscan(in, &v, &w, &d)		if !merge(v-1, w-1, d) {			Fprint(out, "No")			return		}	} 	ccOddDegCnt := make([]byte, n)	xor := 0	for i, d := range deg {		find(i)		if d > 0 { // 每个点权的计算次数是它在原图上的度数,想要影响所有点权的异或值,度数必须是奇数			ccOddDegCnt[fa[i]] ^= 1 // 连通块中的奇度数点的个数			xor ^= dis[i]		}	} 	for rt, c := range ccOddDegCnt {		if c > 0 { // 连通块里有奇数个奇度数点,dis[i] ^= xor 会影响答案奇数次,才能真正地影响答案			for i, f := range fa {				if f == rt {					dis[i] ^= xor				}			}			break		}	} 	Fprintln(out, "Yes")	for _, e := range es {		Fprint(out, dis[e.x]^dis[e.y], " ")	}} //func main() { CF1788F(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 ↗