結果

問題 No.1424 Ultrapalindrome
ユーザー DrDrpilotDrDrpilot
提出日時 2022-01-30 15:47:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 389 ms / 2,000 ms
コード長 798 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 102,912 KB
最終ジャッジ日時 2024-06-11 08:18:31
合計ジャッジ時間 5,064 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
n=int(input())
g=[[] for _ in range(n)]
in_num=[0]*n
for _ in range(n-1):
    a,b=map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)
cnt_3=0
for i in range(n):
    if len(g[i])>=3:
        cnt_3+=1
if cnt_3>=2:
    print('No')
elif cnt_3==0:
    print('Yes')
else:
    q=deque()
    cnt=0
    for i in range(n):
        if len(g[i])==1:
            q.append((i,-1,0))
            cnt+=1
            if cnt>10:
                break
    D=set()
    while q:
        now,pre,d=q.popleft()
        for to in g[now]:
            if to==pre:
                continue
            if len(g[to])==1:
                D.add(d+1)
                continue
            q.append((to,now,d+1))
    if len(D)==1:
        print('Yes')
    else:
        print('No')
0