Problem solution · Go

Codeforces 1646D — Weight the Tree

Codeforces 1646D — Weight the Tree: 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 1646D — Weight the Tree, 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 1646D.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 1646D — Weight the Tree · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io") // https://github.com/EndlessChengfunc cf1646D(in io.Reader, out io.Writer) {	var n int	Fscan(in, &n)	if n == 2 {		Fprint(out, "2 2\n1 1")		return	} 	g := make([][]int, n)	for range n - 1 {		var v, w int		Fscan(in, &v, &w)		v--		w--		g[v] = append(g[v], w)		g[w] = append(g[w], v)	} 	f := make([][4]int, n)	var dfs func(int, int) (int, int, int, int)	dfs = func(v, fa int) (notChosen, chosen, notChosenS, chosenS int) {		chosen = 1		notChosenS = 1		chosenS = len(g[v])		for _, w := range g[v] {			if w == fa {				continue			}			nc, c, ncs, cs := dfs(w, v)			if nc > c {				notChosen += nc				notChosenS += ncs			} else if nc < c {				notChosen += c				notChosenS += cs			} else {				notChosen += c				notChosenS += min(ncs, cs)			}			chosen += nc			chosenS += ncs		}		f[v] = [4]int{notChosen, chosen, notChosenS, chosenS}		return	}	nc, c, ncs, cs := dfs(0, -1) 	ans := make([]any, n)	var makeAns func(int, int, bool)	makeAns = func(v, fa int, chosen bool) {		if chosen {			ans[v] = len(g[v])		} else {			ans[v] = 1		}		for _, w := range g[v] {			if w != fa {				makeAns(w, v, !chosen && (f[w][0] < f[w][1] || f[w][0] == f[w][1] && f[w][2] > f[w][3]))			}		}		return	}	choose := nc < c || nc == c && ncs > cs	makeAns(0, -1, choose) 	tot := ncs	if choose {		tot = cs	}	Fprintln(out, max(nc, c), tot)	Fprintln(out, ans...)} //func main() { cf1646D(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 ↗