結果

問題 No.1424 Ultrapalindrome
ユーザー 小野寺健小野寺健
提出日時 2021-05-20 16:36:27
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 661 ms / 2,000 ms
コード長 1,041 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 44,252 KB
最終ジャッジ日時 2024-04-18 14:05:23
合計ジャッジ時間 9,147 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
11,136 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 30 ms
10,880 KB
testcase_04 AC 28 ms
10,880 KB
testcase_05 AC 29 ms
10,880 KB
testcase_06 AC 28 ms
10,880 KB
testcase_07 AC 29 ms
10,880 KB
testcase_08 AC 29 ms
10,880 KB
testcase_09 AC 373 ms
28,252 KB
testcase_10 AC 380 ms
28,248 KB
testcase_11 AC 268 ms
23,896 KB
testcase_12 AC 359 ms
27,528 KB
testcase_13 AC 122 ms
15,456 KB
testcase_14 AC 297 ms
24,924 KB
testcase_15 AC 30 ms
11,008 KB
testcase_16 AC 237 ms
22,832 KB
testcase_17 AC 282 ms
24,356 KB
testcase_18 AC 289 ms
20,608 KB
testcase_19 AC 416 ms
25,252 KB
testcase_20 AC 113 ms
14,288 KB
testcase_21 AC 309 ms
23,780 KB
testcase_22 AC 213 ms
18,108 KB
testcase_23 AC 70 ms
13,056 KB
testcase_24 AC 103 ms
14,192 KB
testcase_25 AC 98 ms
13,832 KB
testcase_26 AC 491 ms
27,036 KB
testcase_27 AC 493 ms
35,176 KB
testcase_28 AC 464 ms
35,096 KB
testcase_29 AC 523 ms
38,676 KB
testcase_30 AC 636 ms
40,988 KB
testcase_31 AC 661 ms
44,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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')
0