[RUBY] Résoudre avec ac-library-rb AtCoder Union Find (DSU)

introduction

Ce thème

AtCoder Beginner Contest C - Connect Cities Difficulty: 363

AtCoder Beginner Contest D - Friends Difficulty: 676

Ce thème, Union Find

Cette fois, j'utilise «Union Find (DSU)» dedans. 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

Par rapport à la première source, seule la dernière ligne est modifiée.

Résumé

Site référencé ac-library-rb - GitHub

Recommended Posts

Résoudre avec ac-library-rb AtCoder Union Find (DSU)
Résolution avec Ruby AtCoder ACL Débutant Contest C Union Find (DSU)
Résolution avec Ruby AtCoder ABC177 D Union Find
AtCoder ABC127 D hash à résoudre avec Ruby 2.7.1