結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー universatouniversato
提出日時 2020-01-31 23:06:19
言語 Ruby
(3.3.0)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 1,645 bytes
コンパイル時間 62 ms
コンパイル使用メモリ 11,808 KB
実行使用メモリ 31,024 KB
最終ジャッジ日時 2023-10-17 12:02:34
合計ジャッジ時間 5,499 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
15,976 KB
testcase_01 AC 89 ms
15,976 KB
testcase_02 AC 86 ms
15,976 KB
testcase_03 AC 87 ms
15,976 KB
testcase_04 AC 85 ms
15,976 KB
testcase_05 AC 86 ms
15,976 KB
testcase_06 AC 86 ms
15,976 KB
testcase_07 AC 85 ms
15,976 KB
testcase_08 AC 87 ms
15,976 KB
testcase_09 AC 86 ms
15,976 KB
testcase_10 AC 87 ms
15,972 KB
testcase_11 AC 89 ms
15,976 KB
testcase_12 AC 87 ms
15,976 KB
testcase_13 AC 120 ms
17,152 KB
testcase_14 AC 115 ms
17,152 KB
testcase_15 AC 116 ms
17,152 KB
testcase_16 AC 117 ms
17,152 KB
testcase_17 AC 117 ms
17,152 KB
testcase_18 AC 181 ms
20,320 KB
testcase_19 AC 185 ms
20,232 KB
testcase_20 AC 253 ms
23,360 KB
testcase_21 AC 346 ms
28,104 KB
testcase_22 AC 422 ms
29,528 KB
testcase_23 AC 434 ms
31,024 KB
testcase_24 AC 405 ms
31,024 KB
testcase_25 AC 439 ms
31,024 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Node
  attr_accessor :parent, :rank
 
  def initialize
    @parent = -1 # Positive value means its parent,and Negative value means its size.
    @rank = 0
  end
end
 
class UnionFindTree
  def initialize(n)
    @nodes = (0...n).to_a.map { |i| Node.new }
  end
 
  def find(x)
    return @nodes[x].parent < 0 ? 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[b].parent += @nodes[a].parent
      @nodes[a].parent = b
    else
      @nodes[a].parent += @nodes[b].parent
      @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 size(a)
    -@nodes[find(a)].parent
  end
 
  # 確認用。アルゴリズムとは関係無い
  def parents
    @nodes.map(&:parent)
  end
end
 
# N, M = gets.split.map(&:to_i)
 
class Bridge
  attr_accessor :first, :second
 
  def initialize
    @first = nil
    @second = nil
  end
  
  def nodes=(a)
    a[0], a[1] = a[1], a[0] if a[0] > a[1]
    @first = a[0]
    @second = a[1]
  end
  
end

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

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

z = g.map{|k| k.size}


if uft.size(0) == n or uft.size(1) == n
    puts "Bob"
elsif z.count(0) == 1 and (z.count(2) == n-1 and (uft.size(0)==n-1 or uft.size(1)==n-1))
    puts "Bob"
else
    puts "Alice"
end
0