結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,140 KB
testcase_01 AC 18 ms
8,132 KB
testcase_02 AC 15 ms
8,068 KB
testcase_03 AC 16 ms
8,224 KB
testcase_04 AC 16 ms
8,104 KB
testcase_05 AC 15 ms
8,088 KB
testcase_06 AC 15 ms
8,216 KB
testcase_07 AC 16 ms
8,132 KB
testcase_08 AC 16 ms
8,248 KB
testcase_09 AC 16 ms
8,092 KB
testcase_10 AC 16 ms
8,108 KB
testcase_11 AC 16 ms
8,096 KB
testcase_12 AC 15 ms
8,024 KB
testcase_13 AC 16 ms
8,132 KB
testcase_14 AC 16 ms
8,104 KB
testcase_15 AC 58 ms
10,488 KB
testcase_16 AC 48 ms
9,984 KB
testcase_17 WA -
testcase_18 AC 57 ms
10,440 KB
testcase_19 AC 21 ms
8,512 KB
testcase_20 AC 41 ms
9,620 KB
testcase_21 WA -
testcase_22 AC 46 ms
9,908 KB
testcase_23 WA -
testcase_24 AC 56 ms
10,372 KB
testcase_25 AC 63 ms
10,616 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 920 ms
55,884 KB
testcase_36 AC 1,052 ms
81,432 KB
testcase_37 AC 913 ms
55,308 KB
testcase_38 AC 16 ms
8,068 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 996 ms
104,324 KB
testcase_50 AC 1,231 ms
55,340 KB
testcase_51 AC 1,225 ms
55,496 KB
testcase_52 AC 1,244 ms
55,360 KB
testcase_53 AC 1,258 ms
55,372 KB
testcase_54 AC 1,246 ms
55,412 KB
testcase_55 AC 966 ms
82,972 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 1,076 ms
52,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
setrecursionlimit(10**6)

from heapq import heappop,heappush

def dfs(pos, pre):
    global G, 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