結果

問題 No.1295 木と駒
ユーザー Kiri8128Kiri8128
提出日時 2020-11-23 16:10:10
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,463 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 86,572 KB
実行使用メモリ 116,964 KB
最終ジャッジ日時 2023-09-30 23:53:16
合計ジャッジ時間 14,683 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,680 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 91 ms
71,864 KB
testcase_05 WA -
testcase_06 AC 93 ms
71,824 KB
testcase_07 AC 91 ms
71,648 KB
testcase_08 WA -
testcase_09 AC 90 ms
71,504 KB
testcase_10 AC 89 ms
71,504 KB
testcase_11 WA -
testcase_12 AC 86 ms
71,732 KB
testcase_13 AC 87 ms
71,776 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 313 ms
102,916 KB
testcase_17 AC 314 ms
104,584 KB
testcase_18 AC 311 ms
104,964 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 374 ms
106,344 KB
testcase_35 WA -
testcase_36 AC 299 ms
105,168 KB
testcase_37 AC 305 ms
104,896 KB
testcase_38 AC 322 ms
106,320 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 260 ms
108,100 KB
testcase_42 AC 265 ms
116,696 KB
testcase_43 AC 273 ms
107,728 KB
testcase_44 AC 272 ms
116,964 KB
testcase_45 WA -
testcase_46 AC 279 ms
108,052 KB
testcase_47 WA -
testcase_48 AC 276 ms
107,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque

N = int(input())
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = map(int, input().split())
    x, y = x-1, y-1
    X[x].append(y)
    X[y].append(x)

MI = [min(x) for x in X]
MA = [max(x) for x in X]
MA2 = [max([y for y in x + [-1] if y != ma]) for x, ma in zip(X, MA)]

P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

##### Settings
unit = 0
merge = lambda a, b: a + b

## ↓ この2つは必ず対称的にする(じゃないとバグる)
def adj_bu(a, i, p):
    if a >= 4: return 4
    if a == 0 and p == MI[i]: return 0
    if a == 3 and p != MA[i]: return 4
    if i == MA[p]: return 2
    if i == MA2[p]: return 3
    return 4
def adj_td(a, i, p):
    return adj_bu(a, p, i)

adj_fin = lambda a, i: "Yes" if a <= 2 else "No"
#####

ME = [unit] * N
XX = [0] * N
TD = [unit] * N
for i in R[1:][::-1]:
    p = P[i]
    XX[i] = adj_bu(ME[i], i, p) ####################
    ME[p] = merge(ME[p], XX[i])
XX[R[0]] = adj_fin(ME[R[0]], R[0])

for i in R:
    ac = TD[i]
    for j in X[i]:
        TD[j] = ac
        ac = merge(ac, XX[j])
    ac = unit
    for j in X[i][::-1]:
        TD[j] = adj_td(merge(TD[j], ac), j, i)
        ac = merge(ac, XX[j])
        XX[j] = adj_fin(merge(ME[j], TD[j]), j)

print("\n".join(XX))
0