Skip to content

Instantly share code, notes, and snippets.

@pot-code
Created April 9, 2020 13:14
Show Gist options
  • Save pot-code/f372ea6760eb139cf5d672868ecf5a8b to your computer and use it in GitHub Desktop.
Save pot-code/f372ea6760eb139cf5d672868ecf5a8b to your computer and use it in GitHub Desktop.
[union find] #algorithm
type unionFind[]int
func (uf unionFind) find(i int) int {
if uf[i] != i {
uf[i] = uf.find(uf[i])
}
return uf[i]
}
func (uf unionFind) union(x, y int) {
xRoot, yRoot := uf.find(x), uf.find(y)
uf[yRoot] = xRoot
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment