結果

問題 No.1928 Make a Binary Tree
ユーザー 👑 tamatotamato
提出日時 2022-05-06 22:25:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,041 ms / 3,000 ms
コード長 1,498 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 87,188 KB
実行使用メモリ 154,128 KB
最終ジャッジ日時 2023-09-20 03:26:02
合計ジャッジ時間 31,355 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
71,740 KB
testcase_01 AC 95 ms
71,740 KB
testcase_02 AC 95 ms
71,792 KB
testcase_03 AC 95 ms
71,736 KB
testcase_04 AC 96 ms
71,628 KB
testcase_05 AC 96 ms
71,796 KB
testcase_06 AC 98 ms
71,916 KB
testcase_07 AC 94 ms
71,796 KB
testcase_08 AC 97 ms
71,824 KB
testcase_09 AC 96 ms
71,820 KB
testcase_10 AC 95 ms
71,684 KB
testcase_11 AC 94 ms
71,688 KB
testcase_12 AC 93 ms
71,820 KB
testcase_13 AC 94 ms
71,548 KB
testcase_14 AC 93 ms
71,692 KB
testcase_15 AC 273 ms
84,000 KB
testcase_16 AC 244 ms
82,312 KB
testcase_17 AC 231 ms
81,280 KB
testcase_18 AC 273 ms
83,468 KB
testcase_19 AC 126 ms
78,520 KB
testcase_20 AC 220 ms
81,708 KB
testcase_21 AC 182 ms
79,868 KB
testcase_22 AC 243 ms
82,592 KB
testcase_23 AC 217 ms
80,880 KB
testcase_24 AC 260 ms
82,932 KB
testcase_25 AC 266 ms
83,016 KB
testcase_26 AC 869 ms
141,960 KB
testcase_27 AC 627 ms
118,032 KB
testcase_28 AC 678 ms
121,252 KB
testcase_29 AC 929 ms
152,460 KB
testcase_30 AC 585 ms
114,420 KB
testcase_31 AC 793 ms
128,468 KB
testcase_32 AC 975 ms
149,648 KB
testcase_33 AC 672 ms
118,804 KB
testcase_34 AC 803 ms
128,716 KB
testcase_35 AC 364 ms
144,364 KB
testcase_36 AC 475 ms
147,468 KB
testcase_37 AC 443 ms
152,616 KB
testcase_38 AC 93 ms
71,552 KB
testcase_39 AC 810 ms
147,780 KB
testcase_40 AC 758 ms
151,256 KB
testcase_41 AC 779 ms
146,728 KB
testcase_42 AC 747 ms
150,992 KB
testcase_43 AC 776 ms
145,832 KB
testcase_44 AC 757 ms
146,440 KB
testcase_45 AC 825 ms
150,196 KB
testcase_46 AC 770 ms
149,536 KB
testcase_47 AC 833 ms
150,316 KB
testcase_48 AC 761 ms
146,044 KB
testcase_49 AC 282 ms
142,212 KB
testcase_50 AC 687 ms
135,536 KB
testcase_51 AC 667 ms
137,252 KB
testcase_52 AC 690 ms
136,252 KB
testcase_53 AC 653 ms
137,244 KB
testcase_54 AC 649 ms
137,140 KB
testcase_55 AC 310 ms
135,808 KB
testcase_56 AC 1,041 ms
154,128 KB
testcase_57 AC 1,007 ms
150,848 KB
testcase_58 AC 1,039 ms
151,908 KB
testcase_59 AC 452 ms
144,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def main():
    import sys
    from collections import deque
    from heapq import heappush, heappop
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()

    size = [1] * (N + 1)
    for v in seq:
        for u in child[v]:
            size[v] += size[u]

    pq_list = [[] for _ in range(N+1)]
    for v in seq:
        if not child[v]:
            continue
        for u in child[v]:
            heappush(pq_list[v], (-size[u], u))
        # merge
        for u in child[v]:
            if len(pq_list[v]) < len(pq_list[u]):
                pq_list[v], pq_list[u] = pq_list[u], pq_list[v]
            for s, w in pq_list[u]:
                heappush(pq_list[v], (s, w))
        s1, u1 = heappop(pq_list[v])
        size[v] = 1-s1
        if pq_list[v]:
            s2, u2 = heappop(pq_list[v])
            size[v] += -s2
    print(size[1])
    assert size[1] <= N


if __name__ == '__main__':
    main()
0