Problem solution · Go

Codeforces 1354E — Graph Coloring

Codeforces 1354E — Graph Coloring: a Go solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Dynamic programming
Source
EndlessCheng Codeforces Go
Length
94 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

Dynamic programming

For Codeforces 1354E — Graph Coloring, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 94 lines of Go from the credited upstream file 1354E.go.
  • The implementation visibly relies on sequence storage, cached states.
  • No explicit loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

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 1354E — Graph Coloring · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	"bytes"	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gofunc CF1354E(_r io.Reader, out io.Writer) {	in := bufio.NewReader(_r)	var n, m, n1, n2, n3, v, w int	Fscan(in, &n, &m, &n1, &n2, &n3)	g := make([][]int, n)	for ; m > 0; m-- {		Fscan(in, &v, &w)		v--		w--		g[v] = append(g[v], w)		g[w] = append(g[w], v)	} 	color := make([]int8, n)	cnt := [2]int{}	vs := []int{}	var f func(int, int8) bool	f = func(v int, c int8) bool {		color[v] = c		cnt[c-1]++		vs = append(vs, v)		for _, w := range g[v] {			if color[w] == c || color[w] == 0 && !f(w, 3^c) {				return false			}		}		return true	}	dp := make([]bool, n2+1) // dp[i][j] 表示能否从前 i 个连通块(二分图)中标记 j 个 2(第一维可以滚动掉)	dp[0] = true	from := make([][]int, n)	for i := range from {		from[i] = make([]int, n2+1)	}	cc := [][]int{}	for i, c := range color {		if c != 0 {			continue		}		cnt = [2]int{}		vs = []int{}		if !f(i, 1) {			Fprint(out, "NO")			return		}	o:		for j := n2; j >= 0; j-- { // 分组背包,每组有两个元素,对应当前连通块(二分图)的两部的大小			for k, c := range cnt {				if c <= j && dp[j-c] {					dp[j] = true					from[len(cc)][j] = (j-c)<<1 | k					continue o				}			}			dp[j] = false // 由于我们是滚动数组的写法,dp[i][j] 无法满足时要标记成 false		}		cc = append(cc, vs)	} 	if !dp[n2] {		Fprint(out, "NO")		return	}	Fprintln(out, "YES")	ans := bytes.Repeat([]byte{'3'}, n)	for i, j := len(cc)-1, n2; i >= 0; i-- {		j = from[i][j]		tar := int8(j&1) + 1		j >>= 1		for _, v := range cc[i] {			if color[v] == tar {				n2--				ans[v] = '2'			} else if n1 > 0 {				n1--				ans[v] = '1'			}		}	}	Fprintf(out, "%s", ans)} //func main() { CF1354E(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 ↗