結果

問題 No.1928 Make a Binary Tree
ユーザー tnodinotnodino
提出日時 2022-05-08 15:59:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,445 ms / 3,000 ms
コード長 1,142 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 10,944 KB
実行使用メモリ 104,348 KB
最終ジャッジ日時 2023-09-22 12:35:38
合計ジャッジ時間 42,798 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,088 KB
testcase_01 AC 16 ms
8,160 KB
testcase_02 AC 16 ms
8,092 KB
testcase_03 AC 17 ms
8,088 KB
testcase_04 AC 16 ms
8,156 KB
testcase_05 AC 17 ms
8,116 KB
testcase_06 AC 17 ms
8,072 KB
testcase_07 AC 17 ms
8,164 KB
testcase_08 AC 18 ms
8,288 KB
testcase_09 AC 17 ms
8,080 KB
testcase_10 AC 17 ms
8,212 KB
testcase_11 AC 16 ms
8,152 KB
testcase_12 AC 17 ms
8,152 KB
testcase_13 AC 17 ms
8,152 KB
testcase_14 AC 16 ms
8,084 KB
testcase_15 AC 63 ms
10,560 KB
testcase_16 AC 52 ms
10,020 KB
testcase_17 AC 41 ms
9,656 KB
testcase_18 AC 59 ms
10,404 KB
testcase_19 AC 21 ms
8,528 KB
testcase_20 AC 43 ms
9,720 KB
testcase_21 AC 30 ms
8,908 KB
testcase_22 AC 48 ms
9,884 KB
testcase_23 AC 39 ms
9,604 KB
testcase_24 AC 59 ms
10,372 KB
testcase_25 AC 65 ms
10,556 KB
testcase_26 AC 1,216 ms
50,892 KB
testcase_27 AC 722 ms
34,932 KB
testcase_28 AC 819 ms
37,260 KB
testcase_29 AC 1,445 ms
56,696 KB
testcase_30 AC 651 ms
32,824 KB
testcase_31 AC 905 ms
40,988 KB
testcase_32 AC 1,389 ms
55,208 KB
testcase_33 AC 735 ms
35,492 KB
testcase_34 AC 905 ms
41,340 KB
testcase_35 AC 976 ms
56,024 KB
testcase_36 AC 1,118 ms
81,448 KB
testcase_37 AC 1,085 ms
56,420 KB
testcase_38 AC 17 ms
8,112 KB
testcase_39 AC 1,231 ms
80,624 KB
testcase_40 AC 1,252 ms
80,552 KB
testcase_41 AC 1,252 ms
80,696 KB
testcase_42 AC 1,257 ms
80,724 KB
testcase_43 AC 1,256 ms
80,564 KB
testcase_44 AC 1,244 ms
80,560 KB
testcase_45 AC 1,241 ms
80,588 KB
testcase_46 AC 1,255 ms
80,616 KB
testcase_47 AC 1,252 ms
80,636 KB
testcase_48 AC 1,262 ms
80,548 KB
testcase_49 AC 1,071 ms
104,348 KB
testcase_50 AC 1,338 ms
55,504 KB
testcase_51 AC 1,345 ms
55,484 KB
testcase_52 AC 1,370 ms
55,480 KB
testcase_53 AC 1,367 ms
55,496 KB
testcase_54 AC 1,348 ms
55,392 KB
testcase_55 AC 1,036 ms
83,068 KB
testcase_56 AC 1,391 ms
56,892 KB
testcase_57 AC 1,410 ms
56,768 KB
testcase_58 AC 1,413 ms
56,912 KB
testcase_59 AC 1,186 ms
52,196 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[idx], 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