Approach
Depth-first search
For Codeforces 1672F2 — Checker for Array Shuffling, 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
- 58 lines of Go from the credited upstream file 1672F2.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 cf1672F2(in io.Reader, out io.Writer) {10 var T, n int11o:12 for Fscan(in, &T); T > 0; T-- {13 Fscan(in, &n)14 a := make([]int, n+1)15 for i := 1; i <= n; i++ {16 Fscan(in, &a[i])17 }18 19 g := make([][]int, n+1)20 cnt := make([]int, n+1)21 mx := 122 for i := 1; i <= n; i++ {23 var x int24 Fscan(in, &x)25 g[a[i]] = append(g[a[i]], x)26 cnt[a[i]]++27 if cnt[a[i]] > cnt[mx] {28 mx = a[i]29 }30 }31 32 color := make([]int8, n+1)33 color[mx] = 234 35 var dfs func(int) bool36 dfs = func(x int) bool {37 color[x] = 138 for _, y := range g[x] {39 if color[y] == 1 || color[y] == 0 && !dfs(y) {40 return false41 }42 }43 color[x] = 244 return true45 }46 47 for i := 1; i <= n; i++ {48 if color[i] == 0 && !dfs(i) {49 Fprintln(out, "WA")50 continue o51 }52 }53 Fprintln(out, "AC")54 }55}56 5758