Problem solution · Go

Codeforces 2113F — Two Arrays

Codeforces 2113F — Two Arrays: a Go solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Depth-first search
Source
EndlessCheng Codeforces Go
Length
83 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Codeforces 2113F — Two Arrays, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 83 lines of Go from the credited upstream file 2113F.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.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 2113F — Two Arrays · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // https://github.com/EndlessChengfunc cf2113F(in io.Reader, _w io.Writer) {	out := bufio.NewWriter(_w)	defer out.Flush()	var T int	for Fscan(in, &T); T > 0; T-- {		var n int		Fscan(in, &n)		a := make([]int, n+1)		b := make([]int, n+1)		buc := make([][]int, n*2+1)		for i := 1; i <= n; i++ {			Fscan(in, &a[i])			buc[a[i]] = append(buc[a[i]], i)		}		for i := 1; i <= n; i++ {			Fscan(in, &b[i])			buc[b[i]] = append(buc[b[i]], i+n)		} 		ans := 0		for i := 1; i <= n*2; i++ {			ans += min(len(buc[i]), 2)		}		Fprintln(out, ans) 		g := make([][]int, n*2+1)		for i := 1; i <= n; i++ {			g[i] = append(g[i], i+n)			g[i+n] = append(g[i+n], i)		}		for i := 1; i <= n*2; i++ {			if len(buc[i]) > 1 {				g[buc[i][0]] = append(g[buc[i][0]], buc[i][1])				g[buc[i][1]] = append(g[buc[i][1]], buc[i][0])			}		} 		col := make([]int8, n*2+1)		vis := make([]int8, n*2+1)		var dfs func(int)		dfs = func(x int) {			vis[x] = 1			for _, v := range g[x] {				if vis[v] == 0 {					col[v] = col[x] ^ 1					dfs(v)				}			}		}		for i := 1; i <= n*2; i++ {			if vis[i] == 0 {				dfs(i)			}		} 		for i := 1; i <= n; i++ {			if col[i] != 0 {				a[i], b[i] = b[i], a[i]			}		} 		for i := 1; i <= n; i++ {			Fprint(out, a[i], " ")		}		Fprintln(out)		for i := 1; i <= n; i++ {			Fprint(out, b[i], " ")		}		Fprintln(out)	}} //func main() { cf2113F(bufio.NewReader(os.Stdin), os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗