結果

問題 No.2202 贅沢てりたまチキン
ユーザー simansiman
提出日時 2023-02-04 15:31:28
言語 Ruby
(3.3.0)
結果
AC  
実行時間 710 ms / 2,000 ms
コード長 858 bytes
コンパイル時間 489 ms
コンパイル使用メモリ 11,312 KB
実行使用メモリ 24,972 KB
最終ジャッジ日時 2023-09-16 12:05:16
合計ジャッジ時間 10,661 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
15,140 KB
testcase_01 AC 78 ms
15,084 KB
testcase_02 AC 78 ms
15,252 KB
testcase_03 AC 78 ms
15,296 KB
testcase_04 AC 79 ms
15,284 KB
testcase_05 AC 78 ms
15,244 KB
testcase_06 AC 79 ms
15,084 KB
testcase_07 AC 79 ms
15,296 KB
testcase_08 AC 78 ms
15,200 KB
testcase_09 AC 77 ms
15,028 KB
testcase_10 AC 113 ms
24,720 KB
testcase_11 AC 77 ms
15,240 KB
testcase_12 AC 78 ms
15,296 KB
testcase_13 AC 78 ms
15,260 KB
testcase_14 AC 506 ms
24,780 KB
testcase_15 AC 505 ms
24,824 KB
testcase_16 AC 477 ms
24,760 KB
testcase_17 AC 562 ms
24,768 KB
testcase_18 AC 279 ms
20,120 KB
testcase_19 AC 315 ms
24,892 KB
testcase_20 AC 586 ms
24,780 KB
testcase_21 AC 585 ms
24,972 KB
testcase_22 AC 436 ms
15,376 KB
testcase_23 AC 449 ms
15,768 KB
testcase_24 AC 435 ms
15,512 KB
testcase_25 AC 710 ms
24,892 KB
testcase_26 AC 599 ms
24,916 KB
testcase_27 AC 509 ms
24,928 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class UnionFind
  def initialize(n)
    @size = Array.new(n, 1)
    @rank = Array.new(n, 0)
    @parent = []

    (0..n).each do |i|
      @parent[i] = i
    end
  end

  def find(x)
    if @parent[x] == x
      x
    else
      @parent[x] = find(@parent[x])
    end
  end

  def unite(x, y)
    x = find(x)
    y = find(y)
    return if x == y

    if @rank[x] < @rank[y]
      @parent[x] = y
      @size[y] += @size[x]
    else
      @parent[y] = x
      @size[x] += @size[y]

      @rank[x] += 1 if @rank[x] == @rank[y]
    end
  end

  def same?(x, y)
    find(x) == find(y)
  end

  def size(x)
    @size[find(x)]
  end
end

N, M = gets.split.map(&:to_i)
uf = UnionFind.new(2 * N + 1)

M.times do
  a, b = gets.split.map(&:to_i)

  uf.unite(a, b + N)
  uf.unite(a + N, b)
end

if (1..N).all? { |v| uf.same?(v, v + N) }
  puts 'Yes'
else
  puts 'No'
end
0