← Back

Union-Find (Disjoint Set Union)

Track connected groups with near-constant-time merges and membership checks.

1 / 1
type DSU struct {
    parent []int
    rank   []int
}
type DSU struct {
    parent []int
    rank   []int
}

// NewDSU starts with n singleton sets: each element is its own parent.
type DSU struct {
    parent []int
    rank   []int
}

// NewDSU starts with n singleton sets: each element is its own parent.
func NewDSU(n int) *DSU {
    parent := make([]int, n)
    rank := make([]int, n)
    for i := range parent {
        parent[i] = i
    }
    return &DSU{parent, rank}
}
type DSU struct {
    parent []int
    rank   []int
}

// NewDSU starts with n singleton sets: each element is its own parent.
func NewDSU(n int) *DSU {
    parent := make([]int, n)
    rank := make([]int, n)
    for i := range parent {
        parent[i] = i
    }
    return &DSU{parent, rank}
}

// Find returns the representative of x's set, compressing the path on the way.
type DSU struct {
    parent []int
    rank   []int
}

// NewDSU starts with n singleton sets: each element is its own parent.
func NewDSU(n int) *DSU {
    parent := make([]int, n)
    rank := make([]int, n)
    for i := range parent {
        parent[i] = i
    }
    return &DSU{parent, rank}
}

// Find returns the representative of x's set, compressing the path on the way.
func (d *DSU) Find(x int) int {
    if d.parent[x] != x {
        d.parent[x] = d.Find(d.parent[x]) // path compression
    }
    return d.parent[x]
}
type DSU struct {
    parent []int
    rank   []int
}

// NewDSU starts with n singleton sets: each element is its own parent.
func NewDSU(n int) *DSU {
    parent := make([]int, n)
    rank := make([]int, n)
    for i := range parent {
        parent[i] = i
    }
    return &DSU{parent, rank}
}

// Find returns the representative of x's set, compressing the path on the way.
func (d *DSU) Find(x int) int {
    if d.parent[x] != x {
        d.parent[x] = d.Find(d.parent[x]) // path compression
    }
    return d.parent[x]
}

// Union merges the sets of x and y. Returns false if they were already joined.
type DSU struct {
    parent []int
    rank   []int
}

// NewDSU starts with n singleton sets: each element is its own parent.
func NewDSU(n int) *DSU {
    parent := make([]int, n)
    rank := make([]int, n)
    for i := range parent {
        parent[i] = i
    }
    return &DSU{parent, rank}
}

// Find returns the representative of x's set, compressing the path on the way.
func (d *DSU) Find(x int) int {
    if d.parent[x] != x {
        d.parent[x] = d.Find(d.parent[x]) // path compression
    }
    return d.parent[x]
}

// Union merges the sets of x and y. Returns false if they were already joined.
func (d *DSU) Union(x, y int) bool {
    rx, ry := d.Find(x), d.Find(y)
    if rx == ry {
        return false
    }
}
type DSU struct {
    parent []int
    rank   []int
}

// NewDSU starts with n singleton sets: each element is its own parent.
func NewDSU(n int) *DSU {
    parent := make([]int, n)
    rank := make([]int, n)
    for i := range parent {
        parent[i] = i
    }
    return &DSU{parent, rank}
}

// Find returns the representative of x's set, compressing the path on the way.
func (d *DSU) Find(x int) int {
    if d.parent[x] != x {
        d.parent[x] = d.Find(d.parent[x]) // path compression
    }
    return d.parent[x]
}

// Union merges the sets of x and y. Returns false if they were already joined.
func (d *DSU) Union(x, y int) bool {
    rx, ry := d.Find(x), d.Find(y)
    if rx == ry {
        return false
    }

    // Union by rank: attach the shorter tree under the taller one.
}
type DSU struct {
    parent []int
    rank   []int
}

// NewDSU starts with n singleton sets: each element is its own parent.
func NewDSU(n int) *DSU {
    parent := make([]int, n)
    rank := make([]int, n)
    for i := range parent {
        parent[i] = i
    }
    return &DSU{parent, rank}
}

// Find returns the representative of x's set, compressing the path on the way.
func (d *DSU) Find(x int) int {
    if d.parent[x] != x {
        d.parent[x] = d.Find(d.parent[x]) // path compression
    }
    return d.parent[x]
}

// Union merges the sets of x and y. Returns false if they were already joined.
func (d *DSU) Union(x, y int) bool {
    rx, ry := d.Find(x), d.Find(y)
    if rx == ry {
        return false
    }

    // Union by rank: attach the shorter tree under the taller one.
    if d.rank[rx] < d.rank[ry] {
        rx, ry = ry, rx
    }
    d.parent[ry] = rx
    if d.rank[rx] == d.rank[ry] {
        d.rank[rx]++
    }
    return true // merges + finds run in near-O(1) amortized
}

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.