結果

問題 No.1928 Make a Binary Tree
ユーザー tnodinotnodino
提出日時 2022-05-08 00:02:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,072 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 10,828 KB
実行使用メモリ 104,380 KB
最終ジャッジ日時 2023-09-21 12:16:49
合計ジャッジ時間 43,997 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,104 KB
testcase_01 AC 18 ms
8,096 KB
testcase_02 AC 17 ms
8,176 KB
testcase_03 AC 17 ms
8,180 KB
testcase_04 AC 17 ms
8,108 KB
testcase_05 AC 18 ms
8,288 KB
testcase_06 AC 18 ms
8,044 KB
testcase_07 AC 18 ms
8,028 KB
testcase_08 AC 17 ms
8,048 KB
testcase_09 AC 17 ms
8,232 KB
testcase_10 AC 17 ms
8,120 KB
testcase_11 AC 18 ms
8,156 KB
testcase_12 AC 18 ms
8,160 KB
testcase_13 AC 18 ms
8,052 KB
testcase_14 AC 18 ms
8,060 KB
testcase_15 AC 62 ms
10,520 KB
testcase_16 AC 52 ms
9,968 KB
testcase_17 WA -
testcase_18 AC 61 ms
10,388 KB
testcase_19 AC 22 ms
8,520 KB
testcase_20 AC 43 ms
9,688 KB
testcase_21 WA -
testcase_22 AC 49 ms
9,880 KB
testcase_23 WA -
testcase_24 AC 59 ms
10,280 KB
testcase_25 AC 67 ms
10,648 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 973 ms
55,952 KB
testcase_36 AC 1,112 ms
81,480 KB
testcase_37 AC 967 ms
55,412 KB
testcase_38 AC 17 ms
8,220 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,077 ms
104,380 KB
testcase_50 AC 1,398 ms
55,372 KB
testcase_51 AC 1,369 ms
55,404 KB
testcase_52 AC 1,378 ms
55,360 KB
testcase_53 AC 1,374 ms
55,460 KB
testcase_54 AC 1,367 ms
55,384 KB
testcase_55 AC 1,042 ms
82,992 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 1,176 ms
52,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
setrecursionlimit(10**6)

from heapq import heappop,heappush

def dfs(pos, pre):
    global Vec
    M = len(Vec[pos])
    idx = pos
    for nxt in G[pos]:
        if nxt == pre:
            continue
        P = dfs(nxt, pos)
        heappush(Vec[pos], -P)
        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