結果

問題 No.1928 Make a Binary Tree
ユーザー tamatotamato
提出日時 2022-05-06 22:19:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,076 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 202,340 KB
最終ジャッジ日時 2024-07-05 23:37:29
合計ジャッジ時間 32,999 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
54,656 KB
testcase_01 AC 49 ms
54,784 KB
testcase_02 AC 44 ms
54,528 KB
testcase_03 AC 43 ms
54,272 KB
testcase_04 AC 44 ms
54,528 KB
testcase_05 AC 46 ms
54,144 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 44 ms
54,912 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 435 ms
179,240 KB
testcase_36 AC 628 ms
187,512 KB
testcase_37 WA -
testcase_38 AC 44 ms
54,016 KB
testcase_39 AC 995 ms
196,072 KB
testcase_40 AC 946 ms
195,536 KB
testcase_41 AC 956 ms
195,400 KB
testcase_42 AC 965 ms
195,776 KB
testcase_43 AC 965 ms
195,508 KB
testcase_44 AC 974 ms
195,480 KB
testcase_45 AC 971 ms
196,096 KB
testcase_46 AC 960 ms
195,956 KB
testcase_47 AC 1,025 ms
196,184 KB
testcase_48 AC 977 ms
197,568 KB
testcase_49 AC 358 ms
202,340 KB
testcase_50 AC 905 ms
181,820 KB
testcase_51 AC 860 ms
180,532 KB
testcase_52 AC 877 ms
179,444 KB
testcase_53 AC 838 ms
180,368 KB
testcase_54 AC 835 ms
180,968 KB
testcase_55 AC 376 ms
176,940 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 422 ms
151,532 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