Problem solution · Go

Codeforces 724G — Xor-matic Number of the Graph

Codeforces 724G — Xor-matic Number of the Graph: 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
100 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 724G — Xor-matic Number of the Graph, 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

  • 100 lines of Go from the credited upstream file 724G.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 724G — Xor-matic Number of the Graph · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // https://space.bilibili.com/206214type xorBasis24 struct {	b     [60]int	n, or int} func (b *xorBasis24) insert(v int) {	b.or |= v	for i := len(b.b) - 1; i >= 0; i-- {		if v>>i&1 == 0 {			continue		}		if b.b[i] == 0 {			b.b[i] = v			b.n++			return		}		v ^= b.b[i]	}} func cf724G(_r io.Reader, out io.Writer) {	in := bufio.NewReader(_r)	const mod = 1_000_000_007	pow := func(x, n int) int {		res := 1		for ; n > 0; n /= 2 {			if n%2 > 0 {				res = res * x % mod			}			x = x * x % mod		}		return res	} 	var n, m, ans int	Fscan(in, &n, &m)	type nb struct{ to, wt int }	g := make([][]nb, n)	for ; m > 0; m-- {		var v, w, wt int		Fscan(in, &v, &w, &wt)		v--		w--		g[v] = append(g[v], nb{w, wt})		g[w] = append(g[w], nb{v, wt})	} 	dis := make([]int, n)	for i := range dis {		dis[i] = -1	}	var b xorBasis24	var cnt [60]int	var cv int	var dfs func(int, int)	dfs = func(v, xor int) {		dis[v] = xor		cv++		for i := range cnt {			cnt[i] += xor >> i & 1		}		for _, e := range g[v] {			w := e.to			if dis[w] < 0 {				dfs(w, xor^e.wt)			} else {				b.insert(xor ^ e.wt ^ dis[w])			}		}	}	for i, d := range dis {		if d >= 0 {			continue		}		b = xorBasis24{}		cnt = [60]int{}		cv = 0		dfs(i, 0)		for j, c := range cnt {			if b.or>>j&1 > 0 {				ans = (ans + cv*(cv-1)/2%mod*pow(2, j+b.n-1)) % mod			} else {				ans = (ans + c*(cv-c)%mod*pow(2, j+b.n)) % mod			}		}	}	Fprint(out, ans)} //func main() { cf724G(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 ↗