Approach
Depth-first search
For Codeforces 1707C — DFS Trees, 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
- 107 lines of Go from the credited upstream file 1707C.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 "bytes"6 . "fmt"7 "io"8)9 1011func CF1707C(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)13 var n, m, v, w int14 Fscan(in, &n, &m)15 mst := make([][]int, n)16 type edge struct{ x, y int }17 nonMstEdges := []edge{}18 fa := make([]int, n)19 for i := range fa {20 fa[i] = i21 }22 var find func(int) int23 find = func(x int) int {24 if fa[x] != x {25 fa[x] = find(fa[x])26 }27 return fa[x]28 }29 for i := 0; i < m; i++ {30 Fscan(in, &v, &w)31 v--32 w--33 fv, fw := find(v), find(w)34 if fv != fw {35 mst[v] = append(mst[v], w)36 mst[w] = append(mst[w], v)37 fa[fv] = fw38 } else {39 nonMstEdges = append(nonMstEdges, edge{v, w})40 }41 }42 43 dep := make([]int, n)44 var build func(int, int, int)45 build = func(v, fa, d int) {46 dep[v] = d47 for _, w := range mst[v] {48 if w != fa {49 build(w, v, d+1)50 }51 }52 }53 build(0, -1, 0)54 55 check := make([][]int, n)56 for _, e := range nonMstEdges {57 v, w := e.x, e.y58 if dep[v] < dep[w] { 59 v, w = w, v60 }61 check[v] = append(check[v], w)62 }63 64 diff := make([]int, n)65 stk := make([]int, 0, n)66 inStk := make([]bool, n)67 var dfs func(int, int)68 dfs = func(v, fa int) {69 stk = append(stk, v)70 inStk[v] = true71 for _, w := range check[v] {72 diff[v]++ 73 if inStk[w] { 74 diff[0]++75 diff[stk[dep[w]+1]]-- 76 } else { 77 diff[w]++ 78 }79 }80 for _, w := range mst[v] {81 if w != fa {82 dfs(w, v)83 }84 }85 stk = stk[:len(stk)-1]86 inStk[v] = false87 }88 dfs(0, -1)89 90 ans := bytes.Repeat([]byte{'0'}, n)91 build = func(v, fa, s int) {92 s += diff[v]93 if s == m-n+1 {94 ans[v] = '1'95 }96 for _, w := range mst[v] {97 if w != fa {98 build(w, v, s)99 }100 }101 }102 build(0, -1, 0)103 Fprintf(out, "%s", ans)104}105 106107