結果

問題 No.1928 Make a Binary Tree
ユーザー tnodinotnodino
提出日時 2022-05-07 23:32:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 909 bytes
コンパイル時間 242 ms
コンパイル使用メモリ 10,896 KB
実行使用メモリ 103,992 KB
最終ジャッジ日時 2023-09-21 11:42:20
合計ジャッジ時間 42,191 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,184 KB
testcase_01 AC 16 ms
8,080 KB
testcase_02 AC 16 ms
8,028 KB
testcase_03 AC 15 ms
8,084 KB
testcase_04 AC 16 ms
8,124 KB
testcase_05 AC 16 ms
8,400 KB
testcase_06 AC 16 ms
8,016 KB
testcase_07 AC 16 ms
8,068 KB
testcase_08 AC 15 ms
8,028 KB
testcase_09 AC 16 ms
8,028 KB
testcase_10 AC 15 ms
8,072 KB
testcase_11 AC 15 ms
8,028 KB
testcase_12 AC 15 ms
8,112 KB
testcase_13 AC 15 ms
8,104 KB
testcase_14 AC 16 ms
8,100 KB
testcase_15 AC 56 ms
10,740 KB
testcase_16 AC 46 ms
10,116 KB
testcase_17 WA -
testcase_18 AC 54 ms
10,544 KB
testcase_19 AC 20 ms
8,668 KB
testcase_20 AC 39 ms
9,796 KB
testcase_21 WA -
testcase_22 AC 44 ms
9,952 KB
testcase_23 WA -
testcase_24 AC 53 ms
10,328 KB
testcase_25 AC 61 ms
10,820 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 891 ms
57,396 KB
testcase_36 AC 1,037 ms
82,028 KB
testcase_37 AC 923 ms
58,140 KB
testcase_38 AC 16 ms
8,232 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 994 ms
103,992 KB
testcase_50 AC 1,206 ms
56,904 KB
testcase_51 AC 1,205 ms
57,012 KB
testcase_52 AC 1,201 ms
56,852 KB
testcase_53 AC 1,322 ms
56,884 KB
testcase_54 AC 1,207 ms
56,884 KB
testcase_55 AC 971 ms
83,752 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 1,040 ms
53,808 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]))
        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