結果
問題 | No.1928 Make a Binary Tree |
ユーザー | tamato |
提出日時 | 2022-05-06 22:25:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 738 ms / 3,000 ms |
コード長 | 1,498 bytes |
コンパイル時間 | 184 ms |
コンパイル使用メモリ | 82,156 KB |
実行使用メモリ | 150,712 KB |
最終ジャッジ日時 | 2024-07-05 23:45:21 |
合計ジャッジ時間 | 21,902 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,888 KB |
testcase_01 | AC | 43 ms
54,272 KB |
testcase_02 | AC | 44 ms
54,144 KB |
testcase_03 | AC | 40 ms
54,016 KB |
testcase_04 | AC | 40 ms
54,144 KB |
testcase_05 | AC | 40 ms
54,528 KB |
testcase_06 | AC | 40 ms
54,444 KB |
testcase_07 | AC | 40 ms
54,656 KB |
testcase_08 | AC | 40 ms
54,912 KB |
testcase_09 | AC | 39 ms
55,168 KB |
testcase_10 | AC | 39 ms
54,528 KB |
testcase_11 | AC | 39 ms
54,400 KB |
testcase_12 | AC | 39 ms
54,528 KB |
testcase_13 | AC | 40 ms
54,400 KB |
testcase_14 | AC | 39 ms
54,656 KB |
testcase_15 | AC | 195 ms
81,364 KB |
testcase_16 | AC | 175 ms
80,780 KB |
testcase_17 | AC | 158 ms
79,684 KB |
testcase_18 | AC | 186 ms
80,596 KB |
testcase_19 | AC | 73 ms
73,088 KB |
testcase_20 | AC | 150 ms
79,192 KB |
testcase_21 | AC | 127 ms
78,452 KB |
testcase_22 | AC | 164 ms
80,336 KB |
testcase_23 | AC | 147 ms
79,204 KB |
testcase_24 | AC | 181 ms
80,948 KB |
testcase_25 | AC | 191 ms
81,028 KB |
testcase_26 | AC | 628 ms
138,968 KB |
testcase_27 | AC | 440 ms
114,988 KB |
testcase_28 | AC | 499 ms
118,496 KB |
testcase_29 | AC | 675 ms
148,748 KB |
testcase_30 | AC | 416 ms
112,708 KB |
testcase_31 | AC | 543 ms
125,408 KB |
testcase_32 | AC | 693 ms
146,292 KB |
testcase_33 | AC | 472 ms
115,160 KB |
testcase_34 | AC | 538 ms
125,288 KB |
testcase_35 | AC | 270 ms
142,152 KB |
testcase_36 | AC | 374 ms
143,044 KB |
testcase_37 | AC | 337 ms
150,576 KB |
testcase_38 | AC | 40 ms
54,528 KB |
testcase_39 | AC | 592 ms
147,632 KB |
testcase_40 | AC | 549 ms
144,832 KB |
testcase_41 | AC | 592 ms
148,936 KB |
testcase_42 | AC | 559 ms
145,200 KB |
testcase_43 | AC | 581 ms
146,352 KB |
testcase_44 | AC | 572 ms
145,856 KB |
testcase_45 | AC | 572 ms
144,124 KB |
testcase_46 | AC | 552 ms
143,732 KB |
testcase_47 | AC | 577 ms
143,824 KB |
testcase_48 | AC | 545 ms
146,472 KB |
testcase_49 | AC | 204 ms
140,668 KB |
testcase_50 | AC | 432 ms
132,200 KB |
testcase_51 | AC | 432 ms
135,296 KB |
testcase_52 | AC | 434 ms
133,608 KB |
testcase_53 | AC | 427 ms
135,168 KB |
testcase_54 | AC | 429 ms
135,552 KB |
testcase_55 | AC | 243 ms
133,980 KB |
testcase_56 | AC | 738 ms
150,712 KB |
testcase_57 | AC | 698 ms
148,256 KB |
testcase_58 | AC | 722 ms
149,556 KB |
testcase_59 | AC | 314 ms
140,456 KB |
ソースコード
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()