結果

問題 No.1928 Make a Binary Tree
ユーザー tnodinotnodino
提出日時 2022-05-08 14:52:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,142 bytes
コンパイル時間 946 ms
コンパイル使用メモリ 10,876 KB
実行使用メモリ 104,404 KB
最終ジャッジ日時 2023-09-22 11:02:31
合計ジャッジ時間 38,913 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,052 KB
testcase_01 AC 16 ms
8,280 KB
testcase_02 AC 15 ms
8,096 KB
testcase_03 AC 17 ms
8,224 KB
testcase_04 AC 16 ms
8,104 KB
testcase_05 AC 16 ms
8,136 KB
testcase_06 AC 16 ms
8,140 KB
testcase_07 AC 16 ms
8,096 KB
testcase_08 AC 16 ms
8,068 KB
testcase_09 AC 16 ms
8,136 KB
testcase_10 AC 16 ms
8,100 KB
testcase_11 AC 16 ms
8,132 KB
testcase_12 AC 16 ms
8,096 KB
testcase_13 AC 16 ms
8,148 KB
testcase_14 AC 16 ms
8,148 KB
testcase_15 AC 60 ms
10,580 KB
testcase_16 AC 49 ms
10,028 KB
testcase_17 AC 39 ms
9,552 KB
testcase_18 AC 58 ms
10,488 KB
testcase_19 AC 21 ms
8,636 KB
testcase_20 AC 41 ms
9,664 KB
testcase_21 WA -
testcase_22 AC 47 ms
9,932 KB
testcase_23 AC 38 ms
9,380 KB
testcase_24 WA -
testcase_25 AC 63 ms
10,732 KB
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 935 ms
55,924 KB
testcase_36 AC 1,062 ms
81,480 KB
testcase_37 AC 963 ms
55,336 KB
testcase_38 AC 16 ms
8,164 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 AC 1,027 ms
104,404 KB
testcase_50 AC 1,288 ms
55,372 KB
testcase_51 AC 1,265 ms
55,436 KB
testcase_52 AC 1,254 ms
55,400 KB
testcase_53 AC 1,248 ms
55,424 KB
testcase_54 AC 1,268 ms
55,416 KB
testcase_55 AC 994 ms
83,116 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 1,101 ms
52,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
setrecursionlimit(10**6)

from heapq import heappop,heappush

def dfs(pos, pre):
    global G, Vec
    for nxt in G[pos]:
        if nxt == pre:
            continue
        P = dfs(nxt, pos)
        heappush(Vec[pos], -P)
    M = len(Vec[pos])
    idx = pos
    for nxt in G[pos]:
        if nxt == pre:
            continue
        if len(Vec[nxt]) > M:
            M = len(Vec[nxt])
            idx = nxt
    if idx != pos:
        while Vec[pos]:
            heappush(Vec[idx], heappop(Vec[pos]))
    for nxt in G[pos]:
        if nxt == pre:
            continue
        if nxt == idx:
            continue
        while Vec[nxt]:
            heappush(Vec[pos], heappop(Vec[nxt]))
    Vec[pos] = Vec[idx]
    ret = 1
    if len(Vec[pos]) <= 2:
        while Vec[pos]:
            ret += -heappop(Vec[pos])
    else:
        for _ in range(2):
            ret += -heappop(Vec[pos])
    return ret

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    G[x].append(y)
    G[y].append(x)
Vec = [[] for _ in range(N)]
print(dfs(0, -1))
0