結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー universatouniversato
提出日時 2020-01-31 22:03:45
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,120 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 11,924 KB
実行使用メモリ 21,356 KB
最終ジャッジ日時 2023-10-17 09:50:41
合計ジャッジ時間 7,380 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
16,104 KB
testcase_01 AC 82 ms
16,104 KB
testcase_02 AC 81 ms
16,104 KB
testcase_03 AC 80 ms
16,104 KB
testcase_04 AC 81 ms
16,104 KB
testcase_05 AC 81 ms
16,104 KB
testcase_06 AC 80 ms
16,104 KB
testcase_07 AC 81 ms
16,104 KB
testcase_08 AC 82 ms
16,104 KB
testcase_09 AC 83 ms
16,104 KB
testcase_10 WA -
testcase_11 AC 83 ms
16,104 KB
testcase_12 AC 82 ms
16,104 KB
testcase_13 AC 187 ms
16,936 KB
testcase_14 AC 105 ms
16,936 KB
testcase_15 AC 104 ms
16,936 KB
testcase_16 AC 983 ms
16,936 KB
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Node
  attr_accessor :parent, :rank

  def initialize(n)
    @parent = n
    @rank = 0
  end
end

class UnionFindTree
  def initialize(n)
    @nodes = (0..n).to_a.map { |i| Node.new(i) }
  end

  def find(x)
    return @nodes[x].parent == x ? x : @nodes[x].parent = find(@nodes[x].parent)
  end

  def unite(a, b)
    a = find(a)
    b = find(b)
    return if a == b

    if @nodes[a].rank < @nodes[b].rank
      @nodes[a].parent = b
    else
      @nodes[b].parent = a
      @nodes[a].rank += 1 if @nodes[a].rank == @nodes[b].rank
    end
  end

  def same?(a, b)
    find(a) == find(b)
  end

  # 確認用。アルゴリズムとは関係無い
  def parents
    @nodes.map(&:parent)
  end
end

n = gets.to_i
# n, q = gets.split.map(&:to_i)

uft = UnionFindTree.new(n)
(n-1).times do |i|
  
  x,y = gets.split.map(&:to_i)
  
#   if com == 0
    uft.unite(x, y)
#   else
    # puts uft.same?(x, y) ? 1 : 0
#   end
end

n.times do |i|
    (0...i).each do |j|
        if not uft.same?(i,j)
            puts "Alice"
            exit
            # flag = false
            # break
        end
    end
end

puts "Bob"
0