結果

問題 No.1424 Ultrapalindrome
ユーザー MineMine
提出日時 2021-09-10 22:53:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 383 ms / 2,000 ms
コード長 1,081 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 11,060 KB
実行使用メモリ 35,608 KB
最終ジャッジ日時 2023-09-02 20:18:35
合計ジャッジ時間 6,770 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,540 KB
testcase_01 AC 19 ms
8,540 KB
testcase_02 AC 19 ms
8,616 KB
testcase_03 AC 19 ms
8,704 KB
testcase_04 AC 19 ms
8,676 KB
testcase_05 AC 20 ms
8,484 KB
testcase_06 AC 19 ms
8,572 KB
testcase_07 AC 18 ms
8,680 KB
testcase_08 AC 19 ms
8,544 KB
testcase_09 AC 269 ms
23,336 KB
testcase_10 AC 273 ms
23,468 KB
testcase_11 AC 202 ms
19,808 KB
testcase_12 AC 261 ms
22,704 KB
testcase_13 AC 83 ms
12,324 KB
testcase_14 AC 213 ms
20,752 KB
testcase_15 AC 19 ms
8,628 KB
testcase_16 AC 176 ms
19,100 KB
testcase_17 AC 216 ms
20,284 KB
testcase_18 AC 168 ms
16,828 KB
testcase_19 AC 268 ms
20,660 KB
testcase_20 AC 72 ms
11,612 KB
testcase_21 AC 211 ms
20,044 KB
testcase_22 AC 141 ms
14,476 KB
testcase_23 AC 42 ms
10,220 KB
testcase_24 AC 66 ms
11,604 KB
testcase_25 AC 63 ms
11,028 KB
testcase_26 AC 312 ms
22,340 KB
testcase_27 AC 383 ms
30,468 KB
testcase_28 AC 369 ms
29,980 KB
testcase_29 AC 381 ms
32,996 KB
testcase_30 AC 351 ms
32,432 KB
testcase_31 AC 348 ms
35,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque


def main():
    def BFS(s):
        dist = [-1]*n
        q = deque([])
        q.append(s)
        dist[s] = 0
        while q:
            now = q.popleft()
            for to in d[now]:
                if dist[to] == -1:
                    dist[to] = dist[now] + 1
                    q.append(to)
        return dist

    n = int(input())
    d = defaultdict(list)
    for i in range(n-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        d[a].append(b)
        d[b].append(a)

    ends = []
    center = -1
    for i in range(n):
        if len(d[i]) == 1:
            ends.append(i)
        elif len(d[i]) >= 3:
            if center == -1:
                center = i
            else:
                return False
    if center == -1:
        return True
    length = -1
    dist = BFS(center)
    for end in ends:
        if length == -1:
            length = dist[end]
        elif length != dist[end]:
            return False
    return True


if main():
    print("Yes")
else:
    print("No")
0