Problem solution · Go

Codeforces 835F — Roads in the Kingdom

Codeforces 835F — Roads in the Kingdom: 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
112 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 835F — Roads in the Kingdom, 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

  • 112 lines of Go from the credited upstream file 835F.go.
  • The implementation visibly relies on sequence storage.
  • 1 loop block 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 835F — Roads in the Kingdom · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io") // https://github.com/EndlessChengfunc cf835F(in io.Reader, out io.Writer) {	var n, diam, maxInnerDiam int	Fscan(in, &n)	type nb struct{ to, wt int }	g := make([][]nb, n)	deg := make([]int, n)	for range n {		var v, w, wt int		Fscan(in, &v, &w, &wt)		v--		w--		g[v] = append(g[v], nb{w, wt})		g[w] = append(g[w], nb{v, wt})		deg[v]++		deg[w]++	} 	q := []int{}	for i, d := range deg {		if d == 1 {			q = append(q, i)		}	}	for len(q) > 0 {		v := q[0]		q = q[1:]		for _, e := range g[v] {			w := e.to			deg[w]--			if deg[w] == 1 {				q = append(q, w)			}		}	} 	cycle := []nb{}	for start, d := range deg {		if d > 1 {			pre := -1			cur := start			for {				cycle = append(cycle, nb{cur, 0})				for _, e := range g[cur] {					w := e.to					if w != pre && deg[w] > 1 {						cycle[len(cycle)-1].wt = e.wt						pre = cur						cur = w						break					}				}				if cur == start {					break				}			}			break		}	}	m := len(cycle) 	var dfs func(int, int) int	dfs = func(v, fa int) (maxL int) {		for _, e := range g[v] {			w := e.to			if w != fa && deg[w] < 2 {				subL := dfs(w, v) + e.wt				diam = max(diam, maxL+subL)				maxL = max(maxL, subL)			}		}		return	}	maxL := make([]int, m)	for i, e := range cycle {		diam = 0		maxL[i] = dfs(e.to, -1)		maxInnerDiam = max(maxInnerDiam, diam)	} 	suf := make([]int, m+1)	suf2 := make([]int, m)	s, mx := 0, maxL[m-1]	for i := m - 2; i >= 0; i-- {		suf[i+1] = max(suf[i+2], maxL[i+1]+s)		s += cycle[i].wt		suf2[i] = max(suf2[i+1], maxL[i]+s+mx)		mx = max(mx, maxL[i]-s)	}	ans := suf2[0] 	pre, pre2, s, mx := 0, 0, 0, maxL[0]	for i := range m - 1 {		pre = max(pre, maxL[i]+s)		ans = min(ans, max(pre2, suf2[i+1], pre+cycle[m-1].wt+suf[i+1]))		s += cycle[i].wt		pre2 = max(pre2, maxL[i+1]+s+mx)		mx = max(mx, maxL[i+1]-s)	} 	Fprint(out, max(ans, maxInnerDiam))} //func main() { cf835F(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 ↗