結果
| 問題 | 
                            No.1424 Ultrapalindrome
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2021-03-13 09:31:14 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 171 ms / 2,000 ms | 
| コード長 | 696 bytes | 
| コンパイル時間 | 148 ms | 
| コンパイル使用メモリ | 81,952 KB | 
| 実行使用メモリ | 98,080 KB | 
| 最終ジャッジ日時 | 2024-10-15 02:48:27 | 
| 合計ジャッジ時間 | 4,577 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 29 | 
ソースコード
from collections import deque
n = int(input())
e = [[] for i in range(n+1)]
for i in range(n-1):
    a,b = map(int,input().split())
    e[a].append(b)
    e[b].append(a)
c = 0
cand = []
ind = 0
for i in range(1,n+1):
    if len(e[i]) == 1:
        cand.append(i)
    if len(e[i]) >= 3:
        c += 1
        ind = i
if c == 0:
    print("Yes")
    exit()
if c >= 2:
    print("No")
    exit()
dis = [10**10]*(n+1)
dis[ind] = 0
q = deque([ind])
while q:
    now = q.popleft()
    for nex in e[now]:
        if dis[nex] > dis[now]+1:
            dis[nex] = dis[now]+1
            q.append(nex)
d = dis[cand[0]]
for i in cand:
    if d != dis[i]:
        print("No")
        exit()
print("Yes")