結果

問題 No.1424 Ultrapalindrome
ユーザー 小野寺健小野寺健
提出日時 2021-05-20 15:40:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 979 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 30,284 KB
最終ジャッジ日時 2024-04-18 14:04:29
合計ジャッジ時間 8,265 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
16,512 KB
testcase_01 AC 33 ms
10,880 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 28 ms
11,008 KB
testcase_04 AC 28 ms
10,880 KB
testcase_05 AC 29 ms
10,880 KB
testcase_06 AC 28 ms
11,008 KB
testcase_07 AC 29 ms
10,880 KB
testcase_08 AC 30 ms
10,880 KB
testcase_09 AC 566 ms
30,160 KB
testcase_10 AC 555 ms
30,284 KB
testcase_11 AC 366 ms
25,164 KB
testcase_12 AC 602 ms
29,308 KB
testcase_13 AC 148 ms
15,820 KB
testcase_14 AC 422 ms
26,308 KB
testcase_15 AC 30 ms
10,880 KB
testcase_16 AC 335 ms
23,652 KB
testcase_17 AC 389 ms
25,620 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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