結果
| 問題 | 
                            No.1424 Ultrapalindrome
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-12-30 18:41:07 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 810 ms / 2,000 ms | 
| コード長 | 708 bytes | 
| コンパイル時間 | 645 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 35,328 KB | 
| 最終ジャッジ日時 | 2024-10-14 16:04:45 | 
| 合計ジャッジ時間 | 10,354 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 29 | 
ソースコード
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")