Approach
Depth-first search
For Codeforces 1770D — Koxia and Game, 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
- 62 lines of Go from the credited upstream file 1770D.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 CF1770D(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 const mod = 99824435315 16 var T, n, w int17 for Fscan(in, &T); T > 0; T-- {18 Fscan(in, &n)19 g := make([][]int, n)20 a := make([]int, n)21 for i := range a {22 Fscan(in, &a[i])23 }24 for _, v := range a {25 v--26 Fscan(in, &w)27 w--28 g[v] = append(g[v], w)29 g[w] = append(g[w], v)30 }31 32 vis := make([]bool, n)33 c := 034 var dfs func(int, int)35 dfs = func(v, fa int) {36 vis[v] = true37 for _, w := range g[v] {38 if w != fa {39 if w == v {40 c = n41 } else if vis[w] {42 c = 243 } else {44 dfs(w, v)45 }46 }47 }48 }49 ans := int64(1)50 for i, b := range vis {51 if !b {52 c = 053 dfs(i, -1)54 ans = ans * int64(c) % mod55 }56 }57 Fprintln(out, ans)58 }59}60 6162