Problem solution · Go

Codeforces 277E — Binary Tree on Plane

Codeforces 277E — Binary Tree on Plane: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
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

Direct simulation

For Codeforces 277E — Binary Tree on Plane, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 112 lines of Go from the credited upstream file 277E.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

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 277E — Binary Tree on Plane · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io"	"math") /*流量可以用来“选择”或“计数”。我们可以用 1 单位的流量代表“选择一条边”。因此,要构成一棵树,我们总共需要选择 n-1 条边,这意味着网络中的总流量应该是 n-1。 容量可以用来施加“约束”。例如,一个节点的出度不能超过 2,就可以通过设置相关边的容量来实现。 费用可以用来衡量“成本”。边的长度自然就是我们想要最小化的成本。*/ // https://github.com/EndlessChengfunc cf277E(in io.Reader, out io.Writer) {	var n int	Fscan(in, &n)	a := make([]struct{ x, y int }, n)	for i := range a {		Fscan(in, &a[i].x, &a[i].y)	} 	S := n * 2	T := S + 1	type nb struct {		to, rid, cap int		cost         float64	}	g := make([][]nb, T+1)	addEdge := func(from, to, cap int, cost float64) {		g[from] = append(g[from], nb{to, len(g[to]), cap, cost})		g[to] = append(g[to], nb{from, len(g[from]) - 1, 0, -cost})	}	for i, p := range a {		addEdge(S, i, 2, 0)		addEdge(n+i, T, 1, 0)		for j, q := range a {			if p.y > q.y {				addEdge(i, n+j, 1, math.Sqrt(float64((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y))))			}		}	} 	dis := make([]float64, len(g))	type vi struct{ v, i int }	fa := make([]vi, len(g))	inQ := make([]bool, len(g))	spfa := func() bool {		for i := range dis {			dis[i] = math.MaxFloat64		}		dis[S] = 0		inQ[S] = true		q := []int{S}		for len(q) > 0 {			v := q[0]			q = q[1:]			inQ[v] = false			for i, e := range g[v] {				if e.cap == 0 {					continue				}				w := e.to				newD := dis[v] + e.cost				if newD < dis[w] {					dis[w] = newD					fa[w] = vi{v, i}					if !inQ[w] {						inQ[w] = true						q = append(q, w)					}				}			}		}		return dis[T] < math.MaxFloat64	}	maxFlow := 0	minCost := 0.	for spfa() {		minF := math.MaxInt		for v := T; v != S; {			p := fa[v]			minF = min(minF, g[p.v][p.i].cap)			v = p.v		}		for v := T; v != S; {			p := fa[v]			e := &g[p.v][p.i]			e.cap -= minF			g[v][e.rid].cap += minF			v = p.v		}		maxFlow += minF		minCost += dis[T] * float64(minF)	} 	if maxFlow == n-1 {		Fprintf(out, "%.6f", minCost)	} else {		Fprint(out, -1)	}} //func main() { cf277E(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 ↗