結果
| 問題 |
No.1424 Ultrapalindrome
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-05-20 16:36:27 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 687 ms / 2,000 ms |
| コード長 | 1,041 bytes |
| コンパイル時間 | 123 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 44,116 KB |
| 最終ジャッジ日時 | 2024-10-10 07:17:13 |
| 合計ジャッジ時間 | 8,863 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
import heapq
from collections import defaultdict
def dijkstra(s):
global N, P, Leaf
D = [float('inf')] * N
D[s] = 0
q = [(0, s)]
heapq.heapify(q)
while len(q) > 0:
cost, c = heapq.heappop(q)
if cost > D[c]:
continue
for p in Edge[c]:
if D[p] > D[c] + 1:
D[p] = D[c] + 1
heapq.heappush(q, (D[p], p))
res = -1
for e in Leaf:
if res < 0:
res = D[e]
elif res != D[e]:
return -1
return res
N = int(input())
Edge = defaultdict(list)
for _ in range(N-1):
v, u = map(int, input().split())
Edge[v-1].append(u-1)
Edge[u-1].append(v-1)
Leaf = []
Start = -1
NG = False
for k, v in Edge.items():
if len(v) == 1:
Leaf.append(k)
elif len(v) >= 3:
if Start < 0:
Start = k
else:
NG = True
break
if NG:
print('No')
elif Start < 0:
print('Yes')
else:
print('Yes' if dijkstra(Start) >= 0 else 'No')