const std = @import("std");

/// Disjoint-set forest over 0 .. n-1.
///
/// One packed array of i32: a non-root stores its parent (>= 0), a root
/// stores the negated size of its set (< 0). Union by size, path halving.
pub const UnionFind = struct {
    allocator: std.mem.Allocator,
    slots: []i32,
    count: u32,

    pub fn init(allocator: std.mem.Allocator, n: u32) std.mem.Allocator.Error!UnionFind {
        const slots = try allocator.alloc(i32, n);
        @memset(slots, -1);
        return .{ .allocator = allocator, .slots = slots, .count = n };
    }

    pub fn deinit(self: *UnionFind) void {
        self.allocator.free(self.slots);
        self.* = undefined;
    }

    /// Root of x, with path halving.
    inline fn root(a: [*]i32, x0: u32) u32 {
        var x = x0;
        while (true) {
            const p = a[x];
            if (p < 0) return x;
            const up: u32 = @intCast(p);
            const g = a[up];
            if (g < 0) return up;
            a[x] = g;
            x = @intCast(g);
        }
    }

    /// Roots of x and y, walking both paths in lockstep (with path halving)
    /// so that the cache misses of the two searches overlap.
    inline fn roots2(a: [*]i32, x0: u32, y0: u32) [2]u32 {
        var x = x0;
        var y = y0;
        while (true) {
            const px = a[x];
            const py = a[y];
            if (px < 0) return .{ x, root(a, y) };
            if (py < 0) return .{ root(a, x), y };
            const ux: u32 = @intCast(px);
            const uy: u32 = @intCast(py);
            const gx = a[ux];
            const gy = a[uy];
            if (gx < 0) {
                x = ux;
            } else {
                a[x] = gx;
                x = @intCast(gx);
            }
            if (gy < 0) {
                y = uy;
            } else {
                a[y] = gy;
                y = @intCast(gy);
            }
        }
    }

    pub fn find(self: *UnionFind, x: u32) u32 {
        return root(self.slots.ptr, x);
    }

    pub fn unite(self: *UnionFind, a: u32, b: u32) bool {
        const s = self.slots.ptr;
        const r = roots2(s, a, b);
        var ra = r[0];
        var rb = r[1];
        if (ra == rb) return false;
        // s[r] = -size: the larger set has the more negative value.
        if (s[ra] > s[rb]) std.mem.swap(u32, &ra, &rb);
        s[ra] += s[rb];
        s[rb] = @intCast(ra);
        self.count -= 1;
        return true;
    }

    pub fn connected(self: *UnionFind, a: u32, b: u32) bool {
        const r = roots2(self.slots.ptr, a, b);
        return r[0] == r[1];
    }

    pub fn setSize(self: *UnionFind, x: u32) u32 {
        const s = self.slots.ptr;
        const r = root(s, x);
        return @intCast(-s[r]);
    }

    pub fn sets(self: *const UnionFind) u32 {
        return self.count;
    }

    pub fn len(self: *const UnionFind) u32 {
        return @intCast(self.slots.len);
    }

    pub fn parent(self: *const UnionFind, x: u32) u32 {
        const p = self.slots[x];
        return if (p < 0) x else @intCast(p);
    }
};
