結果

問題 No.1928 Make a Binary Tree
ユーザー tamatotamato
提出日時 2022-05-06 22:19:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,076 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 87,284 KB
実行使用メモリ 206,740 KB
最終ジャッジ日時 2023-09-20 03:17:33
合計ジャッジ時間 37,093 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,972 KB
testcase_01 AC 94 ms
71,908 KB
testcase_02 AC 93 ms
71,820 KB
testcase_03 AC 94 ms
71,708 KB
testcase_04 AC 93 ms
71,544 KB
testcase_05 AC 93 ms
71,624 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 94 ms
71,560 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
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 427 ms
169,176 KB
testcase_36 AC 663 ms
191,224 KB
testcase_37 WA -
testcase_38 AC 94 ms
71,772 KB
testcase_39 AC 1,075 ms
202,716 KB
testcase_40 AC 1,022 ms
201,688 KB
testcase_41 AC 1,030 ms
202,232 KB
testcase_42 AC 1,019 ms
201,092 KB
testcase_43 AC 1,015 ms
199,044 KB
testcase_44 AC 1,036 ms
200,052 KB
testcase_45 AC 1,056 ms
206,508 KB
testcase_46 AC 1,038 ms
206,740 KB
testcase_47 AC 1,101 ms
200,640 KB
testcase_48 AC 1,054 ms
198,620 KB
testcase_49 AC 381 ms
194,928 KB
testcase_50 AC 986 ms
185,852 KB
testcase_51 AC 952 ms
183,160 KB
testcase_52 AC 951 ms
182,644 KB
testcase_53 AC 955 ms
183,352 KB
testcase_54 AC 906 ms
184,520 KB
testcase_55 AC 419 ms
186,896 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 457 ms
149,556 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)]
    adj_new = [[] 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])
        adj_new[v].append(u1)
        adj_new[u1].append(v)
        size[v] = 1-s1
        if pq_list[v]:
            s2, u2 = heappop(pq_list[v])
            adj_new[v].append(u2)
            adj_new[u2].append(v)
            size[v] += -s2

    adj = adj_new
    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()
    print(len(seq))


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