結果

問題 No.1928 Make a Binary Tree
ユーザー sotanishysotanishy
提出日時 2022-05-06 22:25:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 844 ms / 3,000 ms
コード長 2,064 bytes
コンパイル時間 426 ms
コンパイル使用メモリ 87,256 KB
実行使用メモリ 161,824 KB
最終ジャッジ日時 2023-09-20 03:24:44
合計ジャッジ時間 28,664 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,116 KB
testcase_01 AC 73 ms
71,244 KB
testcase_02 AC 72 ms
71,576 KB
testcase_03 AC 71 ms
71,596 KB
testcase_04 AC 72 ms
71,308 KB
testcase_05 AC 70 ms
71,324 KB
testcase_06 AC 70 ms
71,548 KB
testcase_07 AC 72 ms
71,556 KB
testcase_08 AC 76 ms
75,680 KB
testcase_09 AC 76 ms
75,972 KB
testcase_10 AC 72 ms
71,380 KB
testcase_11 AC 76 ms
71,560 KB
testcase_12 AC 74 ms
71,596 KB
testcase_13 AC 72 ms
71,152 KB
testcase_14 AC 71 ms
71,304 KB
testcase_15 AC 210 ms
80,928 KB
testcase_16 AC 192 ms
80,800 KB
testcase_17 AC 177 ms
79,900 KB
testcase_18 AC 206 ms
80,572 KB
testcase_19 AC 121 ms
78,092 KB
testcase_20 AC 190 ms
80,380 KB
testcase_21 AC 159 ms
79,132 KB
testcase_22 AC 199 ms
80,556 KB
testcase_23 AC 196 ms
79,876 KB
testcase_24 AC 206 ms
81,096 KB
testcase_25 AC 211 ms
81,360 KB
testcase_26 AC 712 ms
112,032 KB
testcase_27 AC 503 ms
99,428 KB
testcase_28 AC 502 ms
100,548 KB
testcase_29 AC 752 ms
118,116 KB
testcase_30 AC 481 ms
99,520 KB
testcase_31 AC 607 ms
109,236 KB
testcase_32 AC 759 ms
116,260 KB
testcase_33 AC 508 ms
99,668 KB
testcase_34 AC 603 ms
109,612 KB
testcase_35 AC 499 ms
113,168 KB
testcase_36 AC 586 ms
142,588 KB
testcase_37 AC 483 ms
116,772 KB
testcase_38 AC 72 ms
71,476 KB
testcase_39 AC 739 ms
126,040 KB
testcase_40 AC 771 ms
126,260 KB
testcase_41 AC 760 ms
123,536 KB
testcase_42 AC 763 ms
126,236 KB
testcase_43 AC 734 ms
123,252 KB
testcase_44 AC 779 ms
122,808 KB
testcase_45 AC 755 ms
123,136 KB
testcase_46 AC 763 ms
126,196 KB
testcase_47 AC 770 ms
126,028 KB
testcase_48 AC 759 ms
123,672 KB
testcase_49 AC 551 ms
133,868 KB
testcase_50 AC 694 ms
114,172 KB
testcase_51 AC 691 ms
113,688 KB
testcase_52 AC 678 ms
113,632 KB
testcase_53 AC 674 ms
113,636 KB
testcase_54 AC 686 ms
113,252 KB
testcase_55 AC 597 ms
143,156 KB
testcase_56 AC 831 ms
116,708 KB
testcase_57 AC 844 ms
116,804 KB
testcase_58 AC 802 ms
116,736 KB
testcase_59 AC 584 ms
161,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class SegmentTree:
    def __init__(self, size, op, identity):
        n = 1 << (size - 1).bit_length()
        self.size = n
        self.op = op
        self.identity = identity
        self.node = [identity] * (2 * n)

    def __getitem__(self, k):
        return self.node[k + self.size]

    def build(self, v):
        for k in range(len(v)):
            self.node[k + self.size] = v[k]
        for k in range(self.size - 1, 0, -1):
            self.node[k] = self.op(self.node[2 * k], self.node[2 * k + 1])

    def update(self, k, x):
        k += self.size
        self.node[k] = x
        while k > 1:
            k >>= 1
            self.node[k] = self.op(self.node[2 * k], self.node[2 * k + 1])

    def fold(self, l, r):
        vl = vr = self.identity
        l += self.size
        r += self.size
        while l < r:
            if l & 1:
                vl = self.op(vl, self.node[l])
                l += 1
            if r & 1:
                r -= 1
                vr = self.op(self.node[r], vr)
            l >>= 1
            r >>= 1
        return self.op(vl, vr)

def euler_tour(G, root):
    N = len(G)
    stack = [(root, -1, 1), (root, -1, 0)]
    et = []
    first = [-1] * N
    last = [-1] * N
    k = 0
    while stack:
        v, p, t = stack.pop()
        if t == 0:
            et.append(v)
            first[v] = k
            k += 1
            for c in G[v]:
                if c != p:
                    stack.append((c, v, 1))
                    stack.append((c, v, 0))
        else:
            last[v] = k
    return et, first, last

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
    x, y = map(lambda x: int(x)-1, input().split())
    G[x].append(y)
    G[y].append(x)
et, first, last = euler_tour(G, 0)
st = SegmentTree(N, max, (0, -1))
for v in et[::-1]:
    m = 1
    for _ in range(2):
        x, i = st.fold(first[v], last[v])
        if i != -1:
            m += x
            st.update(i, (0, -1))
    st.update(first[v], (m, first[v]))
print(st[0][0])

0