結果

問題 No.1424 Ultrapalindrome
ユーザー rodea
提出日時 2021-03-18 15:43:33
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 774 ms / 2,000 ms
コード長 747 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 37,504 KB
最終ジャッジ日時 2024-11-16 18:42:16
合計ジャッジ時間 9,551 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue

n = int(input())
G = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

deg3 = 0
cv = -1
for i in range(n):
    if 3 <= len(G[i]):
        deg3 += 1
        cv = i

if deg3 >= 2:
    print("No")
    exit()

if deg3 == 0:
    print("Yes")
    exit()

que = queue.Queue()
que.put([cv, 0])
visited = [False for i in range(n)]
visited[cv] = True
leaf = set()
while not que.empty():
    cv, d = que.get()
    is_leaf = True
    for nv in G[cv]:
        if visited[nv]: continue
        visited[nv] = True
        is_leaf = False
        que.put([nv, d + 1])

    if is_leaf: leaf.add(d)

if len(leaf) == 1:
    print("Yes")
else:
    print("No")
0