結果

問題 No.1424 Ultrapalindrome
ユーザー brthyyjpbrthyyjp
提出日時 2021-03-12 23:02:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,313 bytes
コンパイル時間 484 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 106,908 KB
最終ジャッジ日時 2024-04-22 14:30:12
合計ジャッジ時間 5,261 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,760 KB
testcase_01 AC 41 ms
53,760 KB
testcase_02 AC 37 ms
52,608 KB
testcase_03 AC 39 ms
52,224 KB
testcase_04 AC 42 ms
54,016 KB
testcase_05 WA -
testcase_06 AC 40 ms
53,888 KB
testcase_07 AC 40 ms
54,016 KB
testcase_08 WA -
testcase_09 AC 188 ms
92,164 KB
testcase_10 AC 196 ms
92,264 KB
testcase_11 AC 160 ms
89,720 KB
testcase_12 AC 179 ms
93,492 KB
testcase_13 AC 117 ms
80,000 KB
testcase_14 AC 155 ms
90,296 KB
testcase_15 AC 43 ms
54,528 KB
testcase_16 AC 138 ms
86,776 KB
testcase_17 AC 160 ms
90,152 KB
testcase_18 AC 125 ms
84,180 KB
testcase_19 AC 155 ms
89,296 KB
testcase_20 AC 101 ms
78,592 KB
testcase_21 AC 152 ms
86,088 KB
testcase_22 AC 123 ms
81,920 KB
testcase_23 AC 79 ms
77,952 KB
testcase_24 AC 92 ms
78,724 KB
testcase_25 AC 91 ms
78,080 KB
testcase_26 AC 196 ms
90,736 KB
testcase_27 WA -
testcase_28 AC 112 ms
95,900 KB
testcase_29 AC 113 ms
95,768 KB
testcase_30 AC 138 ms
106,908 KB
testcase_31 AC 139 ms
104,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

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

if n == 2:
    print('Yes')
    exit()

s = []
s.append(0)
parent = [-1]*n
order = []
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if parent[v] == u:
            continue
        s.append(u)
        parent[u] = v
#print(order)
order.reverse()
C = [1]*n
for v in order:
    if parent[v] != -1:
        C[parent[v]] += C[v]
#print(C)
G = []
for v in order:
    for u in g[v]:
        if parent[v] == u:
            continue
        if C[u] > n//2:
            break
    else:
        if n-C[v] <= n//2:
            G.append(v)
#print(G)
L = []
for v in order:
    if len(g[v]) == 1:
        L.append(v)

#print(L)

if len(L) == 2:
    print('Yes')
    exit()

if len(G) == 2:
    print('No')
    exit()

s = G[0]
#print(s)
from collections import deque
q = deque([])
q.append(s)
dist = [-1]*n
dist[s] = 0
while q:
    v = q.popleft()
    for u in g[v]:
        if dist[u] == -1:
            dist[u] = dist[v]+1
            q.append(u)
D = set()
for l in L:
    D.add(dist[l])
#print(D)
if len(D) == 1:
    print('Yes')
else:
    print('No')
0