Approach
Depth-first search
For Codeforces 19E — Fairy, 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
- 68 lines of Go from the credited upstream file 19E.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 910func cf19E(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 var n, m, ts, odd int13 Fscan(in, &n, &m)14 type edge struct{ to, i int }15 g := make([][]edge, n)16 for i := 0; i < m; i++ {17 var v, w int18 Fscan(in, &v, &w)19 v--20 w--21 g[v] = append(g[v], edge{w, i})22 g[w] = append(g[w], edge{v, i})23 }24 25 dfn := make([]int, n)26 dep := make([]int, n)27 diffV := make([]int, n)28 diffE := make([]int, m)29 var dfs func(int, int)30 dfs = func(v, fa int) {31 ts++32 dfn[v] = ts33 for _, e := range g[v] {34 w := e.to35 if dfn[w] == 0 {36 dep[w] = dep[v] ^ 137 dfs(w, v)38 diffE[e.i] = diffV[w]39 diffV[v] += diffV[w]40 } else if w != fa && dfn[w] < dfn[v] { 41 t := 1 - (dep[w]^dep[v])*242 if t > 0 { 43 odd++44 diffE[e.i] = 145 }46 diffV[v] += t47 diffV[w] -= t48 }49 }50 }51 for i, d := range dfn {52 if d == 0 {53 dfs(i, -1)54 }55 }56 57 ans := []any{}58 for i, c := range diffE {59 if odd == 0 || c == odd {60 ans = append(ans, i+1)61 }62 }63 Fprintln(out, len(ans))64 Fprintln(out, ans...)65}66 6768