func (f FromList) Root(n NI) NI {
	for p := f.Paths; ; {
		fr := p[n].From
		if fr < 0 {
			return n
		}
		n = fr
	}
}
Root finds the root of a node in a FromList.
package graph
type FromList struct {
	Paths  []PathEnd  
	Leaves Bits       
	MaxLen int        
}
type PathEnd struct {
	From NI   
	Len  int  
}
func NewFromList(n int) FromList {
	return FromList{Paths: make([]PathEnd, n)}
}
func (f FromList) BoundsOk() (ok bool, n NI) {
	for n, e := range f.Paths {
		if int(e.From) >= len(f.Paths) {
			return false, NI(n)
		}
	}
	return true, -1
}
func (f FromList) CommonStart(a, b NI) NI {
	p := f.Paths
	if p[a].Len < p[b].Len {
		a, b = b, a
	}
	for bl := p[b].Len; p[a].Len > bl; {
		a = p[a].From
		if a < 0 {
			return -1
		}
	}
	for a != b {
		a = p[a].From
		if a < 0 {
			return -1
		}
		b = p[b].From
	}
	return a
}
func (f FromList) Cyclic() (cyclic bool, n NI) {
	var vis Bits
	p := f.Paths
	for i := range p {
		var path Bits
		for n := NI(i); vis.Bit(n) == 0; {
			vis.SetBit(n, 1)
			path.SetBit(n, 1)
			if n = p[n].From; n < 0 {
				break
			}
			if path.Bit(n) == 1 {
				return true, n
			}
		}
	}
	return false, -1
}
func (f FromList) IsolatedNodes() (iso Bits) {
	p := f.Paths
	iso.SetAll(len(p))
	for n, e := range p {
		if e.From >= 0 {
			iso.SetBit(NI(n), 0)
			iso.SetBit(e.From, 0)
		}
	}
	return
}
func (f FromList) PathTo(end NI, p []NI) []NI {
	return PathTo(f.Paths, end, p)
}
func PathTo(paths []PathEnd, end NI, p []NI) []NI {
	n := paths[end].Len
	if n == 0 {
		return nil
	}
	if cap(p) >= n {
		p = p[:n]
	} else {
		p = make([]NI, n)
	}
	for {
		n--
		p[n] = end
		if n == 0 {
			return p
		}
		end = paths[end].From
	}
}
func (f FromList) Preorder(v OkNodeVisitor) bool {
	p := f.Paths
	var done Bits
	var df func(NI) bool
	df = func(n NI) bool {
		done.SetBit(n, 1)
		if fr := p[n].From; fr >= 0 && done.Bit(fr) == 0 {
			df(fr)
		}
		return v(n)
	}
	for n := range f.Paths {
		p[n].Len = 0
	}
	return f.Leaves.Iterate(func(n NI) bool {
		return df(n)
	})
}
func (f *FromList) RecalcLeaves() {
	p := f.Paths
	lv := &f.Leaves
	lv.SetAll(len(p))
	for n := range f.Paths {
		if fr := p[n].From; fr >= 0 {
			lv.SetBit(fr, 0)
		}
	}
}
func (f *FromList) RecalcLen() {
	p := f.Paths
	var setLen func(NI) int
	setLen = func(n NI) int {
		switch {
		case p[n].Len > 0:
			return p[n].Len
		case p[n].From < 0:
			p[n].Len = 1
			return 1
		}
		l := 1 + setLen(p[n].From)
		p[n].Len = l
		return l
	}
	for n := range f.Paths {
		p[n].Len = 0
	}
	f.MaxLen = 0
	f.Leaves.Iterate(func(n NI) bool {
		if l := setLen(NI(n)); l > f.MaxLen {
			f.MaxLen = l
		}
		return true
	})
}
func (f *FromList) ReRoot(n NI) {
	p := f.Paths
	fr := p[n].From
	if fr < 0 {
		return
	}
	p[n].From = -1
	for {
		ff := p[fr].From
		p[fr].From = n
		if ff < 0 {
			return
		}
		n = fr
		fr = ff
	}
}