結果

問題 No.1424 Ultrapalindrome
ユーザー DrDrpilot
提出日時 2022-01-30 15:10:36
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 777 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 29,440 KB
最終ジャッジ日時 2024-06-11 08:17:16
合計ジャッジ時間 7,611 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27 WA * 2
権限があれば一括ダウンロードができます

ソースコード

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()
    for i in range(n):
        if len(g[i])==1:
            q.append((i,0))
            break
    D=set()
    visit=[False]*n
    while q:
        now,d=q.popleft()
        visit[now]=True
        for to in g[now]:
            if visit[to]:
                continue
            if len(g[to])==1:
                D.add(d+1)
                continue
            q.append((to,d+1))
    if len(D)==1:
        print('Yes')
    else:
        print('No')
0