結果

問題 No.1424 Ultrapalindrome
ユーザー nephrologistnephrologist
提出日時 2021-03-12 22:30:24
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,201 bytes
コンパイル時間 573 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 96,640 KB
最終ジャッジ日時 2024-04-22 13:33:50
合計ジャッジ時間 4,134 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,760 KB
testcase_01 WA -
testcase_02 AC 44 ms
53,760 KB
testcase_03 AC 41 ms
53,504 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 42 ms
53,632 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 91 ms
81,024 KB
testcase_19 AC 100 ms
83,328 KB
testcase_20 AC 67 ms
70,016 KB
testcase_21 AC 94 ms
82,560 KB
testcase_22 AC 84 ms
80,256 KB
testcase_23 AC 57 ms
66,176 KB
testcase_24 AC 73 ms
69,888 KB
testcase_25 AC 64 ms
69,632 KB
testcase_26 AC 108 ms
83,968 KB
testcase_27 WA -
testcase_28 AC 93 ms
85,504 KB
testcase_29 WA -
testcase_30 AC 99 ms
96,640 KB
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

input = sys.stdin.buffer.readline

n = int(input())
graph = [[] for _ in range(n)]
deg1 = [0] * n
deg2 = [0] * n
for _ in range(n - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)
    deg1[a] += 1
    deg1[b] += 1

kouho = deque()
haji = []
dist1 = [-1] * n
done = [0] * n
for i in range(n):
    if deg1[i] == 1:
        haji.append(i)


def ok():
    print("Yes")
    exit()


def ng():
    print("No")
    exit()


ok()
if n == 2 or n == 3:
    ok()


# bfs
# print("kouho", kouho)

v = haji[0]
dist1[v] = 0
kouho.append(v)
while kouho:
    v = kouho.popleft()
    d = dist1[v]
    for u in graph[v]:

        if dist1[u] == -1:
            dist1[u] = d + 1
            kouho.append(u)

temp = dist1[haji[1]]

for v in haji[1:]:
    if dist1[v] != temp:
        ng()

kouho = deque()
dist2 = [-1] * n
v = haji[-1]
dist2[v] = 0
kouho.append(v)

while kouho:
    v = kouho.popleft()
    d = dist2[v]
    for u in graph[v]:

        if dist2[u] == -1:
            dist2[u] = d + 1
            kouho.append(u)

temp = dist2[haji[0]]

for v in haji[:-1]:
    if dist2[v] != temp:
        ng()

ok()
0