Problem solution · Go

Codeforces 468B — Two Sets

Codeforces 468B — Two Sets: 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
107 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 468B — Two Sets, 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

  • 107 lines of Go from the credited upstream file 468B.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 468B — Two Sets · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gofunc CF468B(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush() 	var n, x, y int	Fscan(in, &n, &x, &y)	a := make([]int, n)	id := map[int]int{}	for i := range a {		Fscan(in, &a[i])		id[a[i]] = i + 1	} 	m := n * 2	g := make([][]int, m)	rg := make([][]int, m)	add := func(v, w int) {		g[v] = append(g[v], w)		rg[w] = append(rg[w], v)	} 	// 令「v 在集合 A」中为真,「v 在集合 B」中为假	for i, v := range a {		if j := id[x-v] - 1; j >= 0 {			// i 为真则 j 为真			add(i, j)			add(j+n, i+n)		} else {			// i 为假			add(i, i+n)		}		if j := id[y-v] - 1; j >= 0 {			// i 为假则 j 为假			add(i+n, j+n)			add(j, i)		} else {			// i 为真			add(i+n, i)		}	}	// 注:若 v 无法在 A 中且无法在 B 中,即 i 即假又真,这会导致 i 和 i+n 在同一个 SCC 中,见后面的代码	// 也可以在建图时直接判断出这种情况 	vs := make([]int, 0, m)	vis := make([]bool, m)	var dfs func(int)	dfs = func(v int) {		vis[v] = true		for _, w := range g[v] {			if !vis[w] {				dfs(w)			}		}		vs = append(vs, v)	}	for i, b := range vis {		if !b {			dfs(i)		}	}	vis = make([]bool, m)	sccIDs := make([]int, m)	sid := 0	var rdfs func(int)	rdfs = func(v int) {		sccIDs[v] = sid		vis[v] = true		for _, w := range rg[v] {			if !vis[w] {				rdfs(w)			}		}	}	for i := m - 1; i >= 0; i-- {		if v := vs[i]; !vis[v] {			rdfs(v)			sid++		}	} 	ans := make([]interface{}, n)	for i, id := range sccIDs[:n] {		if id == sccIDs[i+n] {			Fprint(out, "NO")			return		}		ans[i] = 0		if id < sccIDs[i+n] {			ans[i] = 1		}	}	Fprintln(out, "YES")	Fprintln(out, ans...)} //func main() { CF468B(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 ↗