結果

問題 No.1424 Ultrapalindrome
ユーザー nephrologistnephrologist
提出日時 2021-03-12 22:41:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,021 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 81,796 KB
実行使用メモリ 118,276 KB
最終ジャッジ日時 2024-10-14 13:15:54
合計ジャッジ時間 5,758 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,272 KB
testcase_01 AC 47 ms
54,144 KB
testcase_02 AC 43 ms
53,632 KB
testcase_03 AC 43 ms
53,504 KB
testcase_04 AC 44 ms
54,144 KB
testcase_05 WA -
testcase_06 AC 45 ms
53,888 KB
testcase_07 AC 44 ms
54,260 KB
testcase_08 WA -
testcase_09 AC 236 ms
95,360 KB
testcase_10 AC 238 ms
95,480 KB
testcase_11 AC 191 ms
88,744 KB
testcase_12 AC 221 ms
94,464 KB
testcase_13 AC 125 ms
80,732 KB
testcase_14 AC 202 ms
90,624 KB
testcase_15 AC 46 ms
54,400 KB
testcase_16 AC 174 ms
88,192 KB
testcase_17 AC 193 ms
89,088 KB
testcase_18 AC 162 ms
86,416 KB
testcase_19 AC 196 ms
88,980 KB
testcase_20 AC 113 ms
78,940 KB
testcase_21 AC 178 ms
86,716 KB
testcase_22 AC 135 ms
82,748 KB
testcase_23 AC 87 ms
77,952 KB
testcase_24 AC 109 ms
78,848 KB
testcase_25 AC 105 ms
78,592 KB
testcase_26 AC 222 ms
92,672 KB
testcase_27 WA -
testcase_28 AC 149 ms
106,752 KB
testcase_29 AC 147 ms
106,696 KB
testcase_30 AC 161 ms
117,456 KB
testcase_31 AC 165 ms
118,276 KB
権限があれば一括ダウンロードができます

ソースコード

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()


# graph and n is necessary
def dfs(start):
    par = [-1] * n
    depth = [-1] * n
    size = [0] * n
    stack = []
    euler = []
    stack.append(~start)
    stack.append(start)
    depth[start] = 0
    while stack:
        v = stack.pop()
        if v >= 0:
            d = depth[v]
            euler.append(v)
            for u in graph[v]:
                if par[v] == u:
                    continue
                par[u] = v
                depth[u] = d + 1
                stack.append(~u)
                stack.append(u)
        else:
            a = ~v
            size[a] = 1
            for u in graph[a]:
                if u == par[a]:
                    continue
                size[a] += size[u]
    return par, size, euler


par, size, euler = dfs(0)

grav = []
for v in euler:
    flag = 1
    if n - size[v] > n // 2:
        continue
    for u in graph[v]:
        if u == par[v]:
            continue
        if size[u] > n // 2:
            flag = 0
    if flag:
        grav.append(v)

assert len(grav) <= 2


# deque is necessary.
# n, graph, start is necessary
def BFS(grav):

    dist = [-1] * n
    que = deque()
    for v in grav:
        que.append(v)
        dist[v] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for u in graph[v]:
            if dist[u] == -1:
                dist[u] = d + 1
                que.append(u)
    return dist


dist = BFS(grav)
temp = dist[haji[0]]
for v in haji:
    if dist[v] != temp:
        ng()
ok()

0