結果

問題 No.1424 Ultrapalindrome
ユーザー 箱星
提出日時 2020-12-30 18:41:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 295 ms / 2,000 ms
コード長 708 bytes
コンパイル時間 241 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 99,748 KB
最終ジャッジ日時 2024-10-14 16:04:34
合計ジャッジ時間 6,443 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue

n = int(input())
adj = [[] for _ in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    adj[a].append(b)
    adj[b].append(a)
lst = []
for i in range(1, n + 1):
    if len(adj[i]) >= 3:
        lst.append(i)
if len(lst) >= 2:
    print("No")
elif len(lst) == 0:
    print("Yes")
else:
    lens = set([])
    que = queue.Queue()
    que.put((lst[0], -1, 0))
    while not que.empty():
        v, p, d = que.get()
        isleaf = True
        for w in adj[v]:
            if w != p:
                isleaf = False
                que.put((w, v, d + 1))
        if isleaf:
            lens.add(d)
    if len(lens) == 1:
        print("Yes")
    else:
        print("No")
0