結果

問題 No.1390 Get together
ユーザー maguroflymagurofly
提出日時 2021-02-12 21:31:27
言語 Ruby
(3.3.0)
結果
AC  
実行時間 593 ms / 2,000 ms
コード長 716 bytes
コンパイル時間 530 ms
コンパイル使用メモリ 11,616 KB
実行使用メモリ 32,936 KB
最終ジャッジ日時 2023-09-27 03:01:45
合計ジャッジ時間 12,776 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,220 KB
testcase_01 AC 80 ms
15,192 KB
testcase_02 AC 80 ms
15,332 KB
testcase_03 AC 90 ms
15,536 KB
testcase_04 AC 89 ms
15,720 KB
testcase_05 AC 91 ms
15,536 KB
testcase_06 AC 90 ms
15,476 KB
testcase_07 AC 88 ms
15,556 KB
testcase_08 AC 89 ms
15,452 KB
testcase_09 AC 93 ms
15,652 KB
testcase_10 AC 83 ms
15,112 KB
testcase_11 AC 83 ms
15,036 KB
testcase_12 AC 81 ms
15,036 KB
testcase_13 AC 81 ms
15,132 KB
testcase_14 AC 83 ms
15,140 KB
testcase_15 AC 82 ms
15,024 KB
testcase_16 AC 561 ms
32,936 KB
testcase_17 AC 506 ms
32,792 KB
testcase_18 AC 593 ms
32,828 KB
testcase_19 AC 558 ms
32,832 KB
testcase_20 AC 550 ms
32,828 KB
testcase_21 AC 544 ms
32,724 KB
testcase_22 AC 558 ms
32,704 KB
testcase_23 AC 566 ms
32,780 KB
testcase_24 AC 559 ms
32,760 KB
testcase_25 AC 561 ms
32,764 KB
testcase_26 AC 559 ms
32,788 KB
testcase_27 AC 565 ms
32,780 KB
testcase_28 AC 561 ms
32,876 KB
testcase_29 AC 558 ms
32,852 KB
testcase_30 AC 556 ms
32,704 KB
testcase_31 AC 557 ms
32,872 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class UnionFind
  def initialize(size); @p, @r = [*0...size], [1] * size; end
  def leader(i); j = i; until i == @p[i]; j, i = i, @p[j] = @p[i]; end; i; end
  def merge(i, j); k, l = leader(i), leader(j); return false if k == l; k, l = l, k if @r[k] < @r[l]; @p[l] = k; @r[k] += @r[l]; true; end
  def same?(i, j); leader(i) == leader(j); end
  def size(i); @r[leader(i)]; end
end

N, M = gets.split.map(&:to_i)
slime2box = Array.new(N) { [] }
uf = UnionFind.new(M)
N.times do
    b, c = gets.split.map { |s| s.to_i - 1 }
    slime2box[c] << b
end

count = 0
slime2box.each do |boxes|
    boxes.each_cons(2) do |(i, j)|
        next if uf.same?(i, j)
        uf.merge(i, j)
        count += 1
    end
end

puts count
0