Solve with Ruby AtCoder ABC177 D UnionFind

Introduction

This theme

AtCoder Beginner Contest D - Friends Difficulty: 676

This theme, Union Find

This is an application of the typical problem * B --Union Find --AtCoder *. Ruby

ruby.rb


class UnionFind
  def initialize(n)
    @parents = Array.new(n, -1)
  end
  def find(x)
    @parents[x] < 0 ? x : @parents[x] = find(@parents[x])
  end
  def parents
    @parents
  end
  def union(x, y)
    x = find(x)
    y = find(y)
    return if x == y
    if @parents[x] > @parents[y]
      x, y = y, x
    end
    @parents[x] += @parents[y]
    @parents[y] = x
  end  
end

n, m = gets.split.map(&:to_i)
u = UnionFind.new(n)
m.times do
  a, b = gets.split.map(&:to_i)
  u.union(a - 1, b - 1)
end
puts -u.parents.min

sub.rb


    @parents = Array.new(n, -1)

    @parents[x] += @parents[y]

Assign the initial value -1 to the array @ parents. Next, add the initial value -1 of the paired array. By repeating this, you can get the number of elements in the set of pairs.

sub.rb


@parents = [-3, 0, -2, 2, 0]

In the case of input example 1, if you look at the negative numbers, you can see that they are divided into groups of 3 and 2. Therefore, the output will be 3, which has the larger group element.

Ruby
Code length(Byte) 548
Execution time(ms) 260
memory(KB) 15824

Summary

Referenced site

Recommended Posts

Solve with Ruby AtCoder ABC177 D UnionFind
AtCoder ABC127 D hash to solve with Ruby 2.7.1
Solving with Ruby and Crystal AtCoder ABC 129 D
Solving with Ruby and Java AtCoder ABC129 D 2D array
[At Coder] Solve the ABC183 D problem with Ruby
[At Coder] Solve the ABC182 D problem with Ruby
Solving with Ruby, Perl and Java AtCoder ABC 136 D Breadth-first search
atcoder ABC70 D problem
Solve Google problems with Ruby
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Solve with ac-library-rb AtCoder Union Find (DSU)
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
[Beginner] Let's solve AtCoder problem with Ruby while looking at the article!
Solve ARC104 D Multiset Mean with Scala, Java, C ++, Ruby, Perl, Elixir
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming
AtCoder Beginner Contest 169 A, B, C with ruby
AtCoder ABC 169 C Floating Point Fits in Ruby
[Competition Pro] Solve the knapsack problem with Ruby
Solve Ruby TGIF
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Install Ruby 3.0.0 with asdf
atcoder ABC113 C problem
Getting Started with Ruby
11th, Classification with Ruby
atcoder ABC115 C problem
Evolve Eevee with Ruby
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
Learning Ruby with AtCoder 10 [1st Algorithm Practical Test DoubleCamelCase Sort]
Learn Ruby with AtCoder Beginners Selection [Coins] Answer with short code
Learning Ruby with AtCoder 13 How to make a two-dimensional array