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.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- 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.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910type xorBasis24 struct {11 b [60]int12 n, or int13}14 15func (b *xorBasis24) insert(v int) {16 b.or |= v17 for i := len(b.b) - 1; i >= 0; i-- {18 if v>>i&1 == 0 {19 continue20 }21 if b.b[i] == 0 {22 b.b[i] = v23 b.n++24 return25 }26 v ^= b.b[i]27 }28}29 30func cf724G(_r io.Reader, out io.Writer) {31 in := bufio.NewReader(_r)32 const mod = 1_000_000_00733 pow := func(x, n int) int {34 res := 135 for ; n > 0; n /= 2 {36 if n%2 > 0 {37 res = res * x % mod38 }39 x = x * x % mod40 }41 return res42 }43 44 var n, m, ans int45 Fscan(in, &n, &m)46 type nb struct{ to, wt int }47 g := make([][]nb, n)48 for ; m > 0; m-- {49 var v, w, wt int50 Fscan(in, &v, &w, &wt)51 v--52 w--53 g[v] = append(g[v], nb{w, wt})54 g[w] = append(g[w], nb{v, wt})55 }56 57 dis := make([]int, n)58 for i := range dis {59 dis[i] = -160 }61 var b xorBasis2462 var cnt [60]int63 var cv int64 var dfs func(int, int)65 dfs = func(v, xor int) {66 dis[v] = xor67 cv++68 for i := range cnt {69 cnt[i] += xor >> i & 170 }71 for _, e := range g[v] {72 w := e.to73 if dis[w] < 0 {74 dfs(w, xor^e.wt)75 } else {76 b.insert(xor ^ e.wt ^ dis[w])77 }78 }79 }80 for i, d := range dis {81 if d >= 0 {82 continue83 }84 b = xorBasis24{}85 cnt = [60]int{}86 cv = 087 dfs(i, 0)88 for j, c := range cnt {89 if b.or>>j&1 > 0 {90 ans = (ans + cv*(cv-1)/2%mod*pow(2, j+b.n-1)) % mod91 } else {92 ans = (ans + c*(cv-c)%mod*pow(2, j+b.n)) % mod93 }94 }95 }96 Fprint(out, ans)97}98 99100