Approach
Depth-first search
For Codeforces 547D — Mike and Fish, 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
- 59 lines of Go from the credited upstream file 547D.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 cf547D(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var n, x, y int16 Fscan(in, &n)17 g := make([][]int, n+1)18 var preX, preY [2e5 + 1]int19 for i := 1; i <= n; i++ {20 Fscan(in, &x, &y)21 if preX[x] == 0 {22 preX[x] = i23 } else {24 g[i] = append(g[i], preX[x])25 g[preX[x]] = append(g[preX[x]], i)26 preX[x] = 027 }28 if preY[y] == 0 {29 preY[y] = i30 } else {31 g[i] = append(g[i], preY[y])32 g[preY[y]] = append(g[preY[y]], i)33 preY[y] = 034 }35 }36 37 colors := make([]int, n+1)38 var dfs func(int, int)39 dfs = func(v, c int) {40 colors[v] = c41 for _, w := range g[v] {42 if colors[w] == 0 {43 dfs(w, 3^c)44 }45 }46 }47 for i := 1; i <= n; i++ {48 if colors[i] == 0 {49 dfs(i, 1)50 }51 }52 53 for _, c := range colors[1:] {54 Fprintf(out, "%c", " br"[c])55 }56}57 5859