結果

問題 No.1424 Ultrapalindrome
ユーザー MineMine
提出日時 2021-09-10 22:44:30
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,080 bytes
コンパイル時間 546 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 106,632 KB
最終ジャッジ日時 2023-09-02 19:45:40
合計ジャッジ時間 8,906 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,496 KB
testcase_01 AC 93 ms
71,860 KB
testcase_02 AC 94 ms
71,724 KB
testcase_03 AC 91 ms
71,664 KB
testcase_04 AC 92 ms
71,812 KB
testcase_05 AC 91 ms
71,648 KB
testcase_06 AC 91 ms
71,444 KB
testcase_07 AC 91 ms
71,656 KB
testcase_08 AC 91 ms
71,572 KB
testcase_09 AC 208 ms
91,724 KB
testcase_10 AC 202 ms
91,732 KB
testcase_11 AC 178 ms
88,248 KB
testcase_12 AC 199 ms
89,700 KB
testcase_13 AC 158 ms
81,012 KB
testcase_14 AC 185 ms
89,436 KB
testcase_15 AC 93 ms
71,496 KB
testcase_16 AC 172 ms
88,280 KB
testcase_17 AC 177 ms
88,724 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 129 ms
77,952 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 235 ms
94,728 KB
testcase_28 AC 197 ms
93,020 KB
testcase_29 WA -
testcase_30 AC 216 ms
105,356 KB
testcase_31 AC 233 ms
106,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque


def main():
    def BFS(s):
        dist = [-1]*n
        q = deque([])
        q.append(s)
        dist[0] = 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