結果
問題 | No.1424 Ultrapalindrome |
ユーザー |
|
提出日時 | 2021-05-20 15:40:12 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 979 bytes |
コンパイル時間 | 271 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 30,284 KB |
最終ジャッジ日時 | 2024-10-10 07:16:19 |
合計ジャッジ時間 | 7,642 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 15 TLE * 1 -- * 13 |
ソースコード
import heapq from collections import defaultdict 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) S = [k for k, v in Edge.items() if len(v) == 1] def dijkstra(s, i): global N, P, S 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 S[i+1:]: if res < 0: res = D[e] elif res != D[e]: return -1 return res res = -1 for i, s in enumerate(S[:-1]): v = dijkstra(s, i) if v < 0: print('No') break elif res < 0: res = v elif res != v: print('No') break else: print('Yes')