結果

問題 No.1424 Ultrapalindrome
ユーザー simansiman
提出日時 2021-12-19 14:05:16
言語 Ruby
(3.3.0)
結果
AC  
実行時間 500 ms / 2,000 ms
コード長 720 bytes
コンパイル時間 792 ms
コンパイル使用メモリ 11,236 KB
実行使用メモリ 33,784 KB
最終ジャッジ日時 2023-10-13 18:43:30
合計ジャッジ時間 8,671 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
15,144 KB
testcase_01 AC 89 ms
15,080 KB
testcase_02 AC 86 ms
15,032 KB
testcase_03 AC 85 ms
15,036 KB
testcase_04 AC 84 ms
15,020 KB
testcase_05 AC 85 ms
15,016 KB
testcase_06 AC 85 ms
15,096 KB
testcase_07 AC 84 ms
15,016 KB
testcase_08 AC 84 ms
14,956 KB
testcase_09 AC 319 ms
24,944 KB
testcase_10 AC 321 ms
24,920 KB
testcase_11 AC 238 ms
21,592 KB
testcase_12 AC 312 ms
23,796 KB
testcase_13 AC 136 ms
16,920 KB
testcase_14 AC 265 ms
21,792 KB
testcase_15 AC 86 ms
15,288 KB
testcase_16 AC 230 ms
21,040 KB
testcase_17 AC 249 ms
21,604 KB
testcase_18 AC 244 ms
21,204 KB
testcase_19 AC 326 ms
22,464 KB
testcase_20 AC 132 ms
16,676 KB
testcase_21 AC 264 ms
20,456 KB
testcase_22 AC 191 ms
18,476 KB
testcase_23 AC 109 ms
15,956 KB
testcase_24 AC 129 ms
16,548 KB
testcase_25 AC 130 ms
16,240 KB
testcase_26 AC 392 ms
23,988 KB
testcase_27 AC 375 ms
24,684 KB
testcase_28 AC 361 ms
24,152 KB
testcase_29 AC 481 ms
27,400 KB
testcase_30 AC 473 ms
33,504 KB
testcase_31 AC 500 ms
33,784 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
E = Hash.new { |h, k| h[k] = [] }

(N - 1).times do
  a, b = gets.split.map(&:to_i)
  E[a] << b
  E[b] << a
end

cnt = E.map { |_, list| list.size }.count { |x| x >= 3 }
if cnt >= 2
  puts 'No'
  exit
elsif cnt == 0
  puts 'Yes'
  exit
end

root = (1..N).find { |v| E[v].size >= 3 }
counter = Hash.new(0)

def bfs(root, counter)
  queue = []
  queue << [root, 0]
  visited = Array.new(N + 1, false)

  until queue.empty?
    v, dist = queue.shift

    next if visited[v]
    visited[v] = true

    if dist > 0 && E[v].size == 1
      counter[dist] += 1
    end

    E[v].each do |u|
      queue << [u, dist + 1]
    end
  end
end

bfs(root, counter)

if counter.size >= 2
  puts 'No'
else
  puts 'Yes'
end
0