Approach
Depth-first search
For Codeforces 1817B — Fish 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
- 78 lines of Go from the credited upstream file 1817B.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 CF1817B(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var T, n, m, v, w int16o:17 for Fscan(in, &T); T > 0; T-- {18 Fscan(in, &n, &m)19 g := make([][]int, n)20 for ; m > 0; m-- {21 Fscan(in, &v, &w)22 v--23 w--24 g[v] = append(g[v], w)25 g[w] = append(g[w], v)26 }27 for i, e := range g {28 if len(e) < 4 {29 continue30 }31 vis := make([]bool, n)32 type pair struct{ x, y int }33 path := []pair{}34 onPath := make([]bool, n)35 var dfs func(int, int) bool36 dfs = func(v, fa int) bool {37 vis[v] = true38 for _, w := range g[v] {39 if w == i && fa != i {40 onPath[v] = true41 path = append(path, pair{v, w})42 return true43 }44 }45 for _, w := range g[v] {46 if !vis[w] && dfs(w, v) {47 onPath[v] = true48 path = append(path, pair{v, w})49 return true50 }51 }52 return false53 }54 if !dfs(i, -1) {55 continue56 }57 c := 258 for _, w := range e {59 if !onPath[w] {60 path = append(path, pair{i, w})61 if c--; c == 0 {62 break63 }64 }65 }66 Fprintln(out, "YES")67 Fprintln(out, len(path))68 for _, e := range path {69 Fprintln(out, e.x+1, e.y+1)70 }71 continue o72 }73 Fprintln(out, "NO")74 }75}76 7778