Solve with ac-library-rb AtCoder Union Find (DSU)

Introduction

This theme

AtCoder Beginner Contest C - Connect Cities Difficulty: 363

AtCoder Beginner Contest D - Friends Difficulty: 676

This theme, Union Find

This time, I am using ʻUnion Find (DSU)` in it. C - Connect Cities

ruby.rb


# frozen_string_literal: true

# Disjoint Set Union
class DSU
  def initialize(n = 0)
    # root node: -1 * component size
    # otherwise: parent
    @n = n
    @parent_or_size = Array.new(n, -1)
  end

  def merge(a, b)
    x = leader(a)
    y = leader(b)
    return x if x == y

    x, y = y, x if -@parent_or_size[x] < -@parent_or_size[y]
    @parent_or_size[x] += @parent_or_size[y]
    @parent_or_size[y] = x
    x
  end

  def same(a, b)
    leader(a) == leader(b)
  end

  def leader(a)
    @parent_or_size[a].negative? ? a : (@parent_or_size[a] = leader(@parent_or_size[a]))
  end

  def size(a)
    -@parent_or_size[leader(a)]
  end

  def groups
    leader_buf = Array.new(@n, 0)
    group_size = Array.new(@n, 0)
    (0...@n).each do |i|
      leader_buf[i] = leader(i)
      group_size[leader_buf[i]] += 1
    end
    result = Array.new(@n) { [] }
    (0...@n).each do |i|
      result[leader_buf[i]].push i
    end
    result.delete_if(&:empty?)
    result
  end

  alias root leader
  alias find leader
  alias unite merge
  alias same? same
end

UnionFind        = DSU
UnionFindTree    = DSU
DisjointSetUnion = DSU

n, m = gets.split.map(&:to_i)
ab = m.times.map { gets.split.map(&:to_i) }

u = UnionFind.new(n)

ab.each do |a, b|
  u.merge(a - 1, b - 1)
end
puts u.groups.size - 1

D - Friends

ruby.rb


# frozen_string_literal: true

# Disjoint Set Union
class DSU
  def initialize(n = 0)
    # root node: -1 * component size
    # otherwise: parent
    @n = n
    @parent_or_size = Array.new(n, -1)
  end

  def merge(a, b)
    x = leader(a)
    y = leader(b)
    return x if x == y

    x, y = y, x if -@parent_or_size[x] < -@parent_or_size[y]
    @parent_or_size[x] += @parent_or_size[y]
    @parent_or_size[y] = x
    x
  end

  def same(a, b)
    leader(a) == leader(b)
  end

  def leader(a)
    @parent_or_size[a].negative? ? a : (@parent_or_size[a] = leader(@parent_or_size[a]))
  end

  def size(a)
    -@parent_or_size[leader(a)]
  end

  def groups
    leader_buf = Array.new(@n, 0)
    group_size = Array.new(@n, 0)
    (0...@n).each do |i|
      leader_buf[i] = leader(i)
      group_size[leader_buf[i]] += 1
    end
    result = Array.new(@n) { [] }
    (0...@n).each do |i|
      result[leader_buf[i]].push i
    end
    result.delete_if(&:empty?)
    result
  end

  alias root leader
  alias find leader
  alias unite merge
  alias same? same
end

UnionFind        = DSU
UnionFindTree    = DSU
DisjointSetUnion = DSU

n, m = gets.split.map(&:to_i)
ab = m.times.map { gets.split.map(&:to_i) }

u = UnionFind.new(n)

ab.each do |a, b|
  u.merge(a - 1, b - 1)
end
puts u.groups.map { _1.size }.max

Compared to the first source, only the last line is modified.

Summary

Referenced site ac-library-rb - GitHub

Recommended Posts

Solve with ac-library-rb AtCoder Union Find (DSU)
Solving with Ruby AtCoder ACL Beginner Contest C Union Find (DSU)
Solve with Ruby AtCoder ABC177 D UnionFind
AtCoder ABC127 D hash to solve with Ruby 2.7.1