結果

問題 No.1928 Make a Binary Tree
ユーザー tnodinotnodino
提出日時 2022-05-08 14:39:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,180 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 10,932 KB
実行使用メモリ 106,004 KB
最終ジャッジ日時 2023-09-22 10:42:44
合計ジャッジ時間 47,187 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,140 KB
testcase_01 AC 17 ms
8,228 KB
testcase_02 AC 17 ms
8,164 KB
testcase_03 AC 17 ms
8,168 KB
testcase_04 AC 16 ms
8,052 KB
testcase_05 AC 19 ms
8,396 KB
testcase_06 AC 17 ms
8,088 KB
testcase_07 AC 18 ms
8,068 KB
testcase_08 AC 17 ms
8,232 KB
testcase_09 AC 18 ms
8,096 KB
testcase_10 AC 17 ms
8,092 KB
testcase_11 AC 16 ms
8,092 KB
testcase_12 AC 17 ms
8,120 KB
testcase_13 AC 17 ms
8,164 KB
testcase_14 AC 17 ms
8,056 KB
testcase_15 AC 62 ms
10,404 KB
testcase_16 AC 51 ms
9,968 KB
testcase_17 AC 41 ms
9,588 KB
testcase_18 AC 62 ms
10,408 KB
testcase_19 AC 22 ms
8,560 KB
testcase_20 AC 43 ms
9,664 KB
testcase_21 WA -
testcase_22 AC 50 ms
9,924 KB
testcase_23 AC 39 ms
9,540 KB
testcase_24 WA -
testcase_25 AC 67 ms
10,716 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 984 ms
57,448 KB
testcase_36 AC 1,119 ms
83,028 KB
testcase_37 AC 1,013 ms
56,944 KB
testcase_38 AC 16 ms
8,396 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,064 ms
106,004 KB
testcase_50 AC 1,364 ms
56,968 KB
testcase_51 AC 1,349 ms
56,952 KB
testcase_52 AC 1,361 ms
57,024 KB
testcase_53 AC 1,351 ms
57,012 KB
testcase_54 AC 1,354 ms
57,068 KB
testcase_55 AC 1,040 ms
84,588 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 1,202 ms
53,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
setrecursionlimit(10**6)

from heapq import heappop,heappush

def dfs(pos, pre):
    global Flg, Vec
    Flg[pos] = True
    for nxt in G[pos]:
        if Flg[nxt]:
            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)
Flg = [False] * N
Vec = [[] for _ in range(N)]
print(dfs(0, -1))
0