結果

問題 No.277 根掘り葉掘り
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2017-03-12 12:03:17
言語 Ruby
(3.3.0)
結果
AC  
実行時間 416 ms / 3,000 ms
コード長 794 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 11,228 KB
実行使用メモリ 33,840 KB
最終ジャッジ日時 2023-09-12 11:52:36
合計ジャッジ時間 8,283 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,148 KB
testcase_01 AC 79 ms
15,304 KB
testcase_02 AC 79 ms
15,312 KB
testcase_03 AC 81 ms
15,196 KB
testcase_04 AC 80 ms
15,248 KB
testcase_05 AC 81 ms
15,304 KB
testcase_06 AC 79 ms
15,164 KB
testcase_07 AC 80 ms
15,148 KB
testcase_08 AC 80 ms
15,088 KB
testcase_09 AC 413 ms
29,888 KB
testcase_10 AC 401 ms
33,840 KB
testcase_11 AC 407 ms
31,524 KB
testcase_12 AC 405 ms
31,496 KB
testcase_13 AC 412 ms
33,540 KB
testcase_14 AC 410 ms
33,120 KB
testcase_15 AC 407 ms
32,536 KB
testcase_16 AC 416 ms
32,532 KB
testcase_17 AC 414 ms
32,700 KB
testcase_18 AC 409 ms
32,676 KB
testcase_19 AC 404 ms
32,524 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 = []
next_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]
    next_queue << t
    checked_nodes[t] = true
    node_lengths[t] = next_root_length
  end
  next unless queue.empty?
  break if next_queue.empty?
  queue = next_queue
  next_queue = []
end

node_lengths.shift
puts node_lengths
0