結果

問題 No.277 根掘り葉掘り
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2017-03-12 12:02:15
言語 Ruby
(3.2.2)
結果
AC  
実行時間 384 ms / 3,000 ms
コード長 702 bytes
コンパイル時間 59 ms
コンパイル使用メモリ 11,576 KB
実行使用メモリ 33,740 KB
最終ジャッジ日時 2023-09-07 11:07:57
合計ジャッジ時間 6,434 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
15,156 KB
testcase_01 AC 73 ms
15,256 KB
testcase_02 AC 72 ms
15,048 KB
testcase_03 AC 72 ms
15,048 KB
testcase_04 AC 73 ms
15,188 KB
testcase_05 AC 73 ms
15,148 KB
testcase_06 AC 78 ms
15,092 KB
testcase_07 AC 73 ms
15,244 KB
testcase_08 AC 71 ms
15,156 KB
testcase_09 AC 384 ms
29,804 KB
testcase_10 AC 358 ms
33,740 KB
testcase_11 AC 376 ms
31,348 KB
testcase_12 AC 381 ms
31,296 KB
testcase_13 AC 381 ms
33,164 KB
testcase_14 AC 378 ms
33,312 KB
testcase_15 AC 376 ms
32,308 KB
testcase_16 AC 377 ms
32,244 KB
testcase_17 AC 384 ms
32,404 KB
testcase_18 AC 379 ms
31,864 KB
testcase_19 AC 381 ms
32,156 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# frozen_string_literal: true
n = gets.to_i
edges = Array.new(n + 1) { [] }
(n - 1).times do
  x, y = gets.split.map(&:to_i)
  edges[x] << y
  edges[y] << x
end

zero_nodes = [1] + edges.each_with_index.drop(2)
  .find_all { |v, _i| v.length == 1 }.map(&:last)

node_lengths = [nil] * (n + 1)
checked_nodes = [false] * (n + 1)

queue = []

zero_nodes.each do |z|
  queue << z
  checked_nodes[z] = true
  node_lengths[z] = 0
end

loop do
  i = queue.shift
  next_root_length = node_lengths[i] + 1
  edges[i].each do |t|
    next if checked_nodes[t]
    queue << t
    checked_nodes[t] = true
    node_lengths[t] = next_root_length
  end
  break if queue.empty?
end

node_lengths.shift
puts node_lengths
0