結果

問題 No.1928 Make a Binary Tree
ユーザー mkawa2mkawa2
提出日時 2022-05-06 22:06:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,460 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 10,832 KB
実行使用メモリ 113,668 KB
最終ジャッジ日時 2023-09-20 03:02:03
合計ジャッジ時間 35,557 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,152 KB
testcase_01 AC 18 ms
8,084 KB
testcase_02 AC 18 ms
8,260 KB
testcase_03 AC 18 ms
8,196 KB
testcase_04 AC 17 ms
8,144 KB
testcase_05 AC 17 ms
8,120 KB
testcase_06 WA -
testcase_07 AC 17 ms
8,188 KB
testcase_08 AC 18 ms
8,204 KB
testcase_09 AC 18 ms
8,204 KB
testcase_10 AC 18 ms
8,160 KB
testcase_11 AC 18 ms
8,160 KB
testcase_12 AC 18 ms
8,184 KB
testcase_13 WA -
testcase_14 AC 18 ms
8,152 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
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 WA -
testcase_35 AC 801 ms
63,020 KB
testcase_36 AC 858 ms
88,384 KB
testcase_37 AC 776 ms
60,548 KB
testcase_38 AC 17 ms
8,132 KB
testcase_39 AC 1,077 ms
86,612 KB
testcase_40 AC 1,068 ms
86,620 KB
testcase_41 AC 1,055 ms
86,420 KB
testcase_42 AC 1,065 ms
86,644 KB
testcase_43 AC 1,065 ms
86,608 KB
testcase_44 AC 1,076 ms
86,424 KB
testcase_45 AC 1,058 ms
86,792 KB
testcase_46 AC 1,049 ms
86,508 KB
testcase_47 AC 1,058 ms
86,532 KB
testcase_48 AC 1,049 ms
86,552 KB
testcase_49 AC 899 ms
113,668 KB
testcase_50 AC 1,118 ms
57,088 KB
testcase_51 AC 1,101 ms
57,112 KB
testcase_52 AC 1,101 ms
57,212 KB
testcase_53 AC 1,111 ms
57,016 KB
testcase_54 AC 1,104 ms
57,120 KB
testcase_55 AC 849 ms
88,592 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 926 ms
56,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

from heapq import *

n = II()
to = [[] for _ in range(n)]
for _ in range(n-1):
    u, v = LI1()
    to[u].append(v)
    to[v].append(u)

size = [1]*n
hps = [[] for _ in range(n)]

def merge(u, v):
    swap = False
    if len(hps[u]) < len(hps[v]):
        u, v = v, u
        swap = True
    for s in hps[v]: heappush(hps[u], s)
    if swap: hps[v] = hps[u]

def dfs(u=0, par=-1):
    ss = []
    for v in to[u]:
        if v == par: continue
        dfs(v, u)
        merge(u, v)
        ss.append(size[v])
    ss.sort(reverse=True)
    size[u] += sum(ss[:2])
    for s in ss[2:]: heappush(hps[u], -s)
    if len(ss) < 2 and hps[u]:
        s = -heappop(hps[u])
        size[u] += s
    # print(u, par, size, hps)

dfs()

print(size[0])
0