結果

問題 No.1424 Ultrapalindrome
ユーザー magurofly
提出日時 2021-03-12 23:06:50
言語 Ruby
(3.4.1)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 675 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 25,728 KB
最終ジャッジ日時 2024-10-14 16:25:01
合計ジャッジ時間 6,340 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
graph = Array.new(N) { [] }
degrees = [0] * N
(N - 1).times do
    u, v = gets.split.map { |x| x.to_i - 1 }
    graph[u] << v
    graph[v] << u
    degrees[u] += 1
    degrees[v] += 1
end

case degrees.count { |d| d > 2 }
when 0
    puts "Yes"
when 1
    center = degrees.index { |d| d > 2 }
    dists = [nil] * N
    dists[center] = 0
    queue = [center]
    until queue.empty?
        u = queue.shift
        graph[u].each do |v|
            next if dists[v]
            dists[v] = dists[u] + 1
            queue << v
        end
    end
    puts ((0 ... N).filter_map { |i| degrees[i] == 1 && dists[i] }.uniq.size == 1) ? "Yes" : "No"
else
    puts "No"
end
0