Approach
Depth-first search
For Codeforces 1985H1 — Maximize the Largest Component (Easy Version), 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
- 70 lines of Go from the credited upstream file 1985H1.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 . "fmt"5 "io"6)7 89func cf1985H1(in io.Reader, out io.Writer) {10 dir4 := []struct{ x, y int }{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}11 var T, n, m, minX, minY, maxX, maxY, sz int12 for Fscan(in, &T); T > 0; T-- {13 Fscan(in, &n, &m)14 a := make([][]byte, n)15 for i := range a {16 Fscan(in, &a[i])17 }18 var dfs func(int, int)19 dfs = func(i, j int) {20 minX = min(minX, i)21 maxX = max(maxX, i)22 minY = min(minY, j)23 maxY = max(maxY, j)24 sz++25 a[i][j] = 026 for _, d := range dir4 {27 x, y := i+d.x, j+d.y28 if 0 <= x && x < n && 0 <= y && y < m && a[x][y] == '#' {29 dfs(x, y)30 }31 }32 }33 cr := make([]int, n)34 cc := make([]int, m)35 dr := make([]int, n+2)36 dc := make([]int, m+2)37 for i, row := range a {38 for j, b := range row {39 if b == '#' {40 minX, minY, maxX, maxY, sz = n, m, 0, 0, 041 dfs(i, j)42 minX = max(minX-1, 0)43 minY = max(minY-1, 0)44 dr[minX] += sz45 dr[maxX+2] -= sz46 dc[minY] += sz47 dc[maxY+2] -= sz48 } else if b == '.' {49 cr[i]++50 cc[j]++51 }52 }53 }54 ans := 055 s := 056 for i, d := range dr[:n] {57 s += d58 ans = max(ans, cr[i]+s)59 }60 s = 061 for j, d := range dc[:m] {62 s += d63 ans = max(ans, cc[j]+s)64 }65 Fprintln(out, ans)66 }67}68 6970