結果

問題 No.1424 Ultrapalindrome
ユーザー yuusanlondonyuusanlondon
提出日時 2021-03-12 21:32:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 891 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 82,240 KB
実行使用メモリ 99,848 KB
最終ジャッジ日時 2024-04-22 16:59:21
合計ジャッジ時間 5,940 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,736 KB
testcase_01 AC 41 ms
52,864 KB
testcase_02 AC 40 ms
52,480 KB
testcase_03 AC 43 ms
52,352 KB
testcase_04 AC 40 ms
52,352 KB
testcase_05 AC 41 ms
52,480 KB
testcase_06 AC 41 ms
52,992 KB
testcase_07 AC 40 ms
53,120 KB
testcase_08 AC 40 ms
52,608 KB
testcase_09 AC 173 ms
85,060 KB
testcase_10 AC 172 ms
84,548 KB
testcase_11 AC 143 ms
82,200 KB
testcase_12 AC 170 ms
84,612 KB
testcase_13 AC 96 ms
77,852 KB
testcase_14 AC 143 ms
82,308 KB
testcase_15 AC 43 ms
53,760 KB
testcase_16 AC 129 ms
81,132 KB
testcase_17 AC 138 ms
81,920 KB
testcase_18 AC 217 ms
83,188 KB
testcase_19 AC 266 ms
85,184 KB
testcase_20 AC 149 ms
79,084 KB
testcase_21 AC 229 ms
83,328 KB
testcase_22 AC 199 ms
81,576 KB
testcase_23 AC 122 ms
78,328 KB
testcase_24 AC 160 ms
78,692 KB
testcase_25 AC 158 ms
78,848 KB
testcase_26 AC 300 ms
86,304 KB
testcase_27 AC 204 ms
89,164 KB
testcase_28 AC 146 ms
87,344 KB
testcase_29 AC 159 ms
89,080 KB
testcase_30 AC 256 ms
99,848 KB
testcase_31 AC 252 ms
99,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
INF=10**9
def Dijkstra(graph, start):
  dist=[INF]*len(graph)
  parent=[INF]*len(graph)
  queue=[(0, start)]
  dist[start]=0
  while queue:
    path_len, v=heapq.heappop(queue)
    if path_len==dist[v]: 
      for w in graph[v]:
        if dist[w]>path_len+1:
          parent[w]=v
          heapq.heappush(queue, (path_len+1, w))
          dist[w]=path_len+1    
  return (dist,parent)
n=int(input())

graph=[]
for i in range(n):
  graph.append([])
for i in range(n-1):
  a,b=map(int,input().split())
  graph[a-1].append(b-1)
  graph[b-1].append(a-1)
flag=0
fail=0
for i in range(n):
  if len(graph[i])>2:
    if flag==1:
      fail=1
    flag=1
    start=i
if fail:
  print('No')
  exit()
if flag==0:
  print('Yes')
  exit()
dist,parent=Dijkstra(graph,start)
k=set()
for i in range(n):
  if len(graph[i])==1:
    k.add(dist[i])
if len(k)==1:
  print('Yes')
else:
  print('No')
0