結果

問題 No.1833 Subway Planning
ユーザー tamatotamato
提出日時 2022-02-04 23:04:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,078 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 219,880 KB
最終ジャッジ日時 2024-06-11 12:41:51
合計ジャッジ時間 18,000 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,632 KB
testcase_01 AC 37 ms
53,248 KB
testcase_02 AC 36 ms
53,376 KB
testcase_03 AC 37 ms
53,376 KB
testcase_04 AC 542 ms
201,164 KB
testcase_05 AC 527 ms
200,608 KB
testcase_06 AC 863 ms
200,588 KB
testcase_07 AC 923 ms
201,656 KB
testcase_08 AC 942 ms
201,400 KB
testcase_09 AC 663 ms
200,604 KB
testcase_10 AC 1,356 ms
219,880 KB
testcase_11 AC 1,219 ms
217,416 KB
testcase_12 AC 1,207 ms
219,464 KB
testcase_13 AC 1,379 ms
217,540 KB
testcase_14 WA -
testcase_15 AC 432 ms
195,916 KB
testcase_16 AC 629 ms
197,836 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 482 ms
194,592 KB
testcase_20 WA -
testcase_21 AC 1,290 ms
203,364 KB
testcase_22 AC 1,126 ms
205,376 KB
testcase_23 AC 38 ms
53,376 KB
testcase_24 AC 37 ms
53,760 KB
testcase_25 AC 37 ms
53,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


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

    class SegmentTree:
        def __init__(self, A, initialize=True, segfunc=max, ident=-20000000000):
            self.N = len(A)
            self.LV = (self.N - 1).bit_length()
            self.N0 = 1 << self.LV
            self.segfunc = segfunc
            self.ident = ident
            if initialize:
                self.data = [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N)
                for i in range(self.N0 - 1, 0, -1):
                    self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1])
            else:
                self.data = [self.ident] * (self.N0 * 2)

        def update(self, i, x):
            i += self.N0 - 1
            self.data[i] = x
            for _ in range(self.LV):
                i >>= 1
                self.data[i] = self.segfunc(self.data[i * 2], self.data[i * 2 + 1])

        def get(self, i):
            return self.data[i + self.N0 - 1]

        # open interval [l, r)
        def query(self, l, r):
            l += self.N0 - 1
            r += self.N0 - 1
            ret_l = self.ident
            ret_r = self.ident
            while l < r:
                if l & 1:
                    ret_l = self.segfunc(ret_l, self.data[l])
                    l += 1
                if r & 1:
                    ret_r = self.segfunc(self.data[r - 1], ret_r)
                    r -= 1
                l >>= 1
                r >>= 1
            return self.segfunc(ret_l, ret_r)

        # return smallest i(l <= i < r) s.t. check(A[i]) == True
        def binsearch(self, l, r, check):
            if not check(self.query(l, r)):
                return r
            l += self.N0 - 1
            val = self.ident
            while True:
                if check(self.segfunc(val, self.data[l])):
                    break
                if l & 1:
                    val = self.segfunc(val, self.data[l])
                    l += 1
                l >>= 1
            while l < self.N0:
                newval = self.segfunc(val, self.data[l * 2])
                if not check(newval):
                    val = newval
                    l = (l << 1) + 1
                else:
                    l <<= 1
            return l - self.N0 + 1

    N = int(input())
    adj = [[] for _ in range(N+1)]
    C = []
    CC = []
    E = {}
    for i in range(N - 1):
        a, b, c = map(int, input().split())
        adj[a].append((b, c))
        adj[b].append((a, c))
        C.append((c, i+1, a, b))
        CC.append(c)
        E[a * (N + 1) + b] = i + 1
        E[b * (N + 1) + a] = i + 1

    C.sort(key=lambda x: x[0], reverse=True)
    ma = C[0][0]
    v_left = C[0][2]
    v_right = C[0][3]
    ST = SegmentTree(CC)
    ST.update(C[0][1], 0)
    seen = [0] * (N+1)
    seen[v_left] = 1
    seen[v_right] = 1
    pq = []
    par = [-1] * (N+1)
    for u, c in adj[v_left]:
        if not seen[u]:
            heappush(pq, (-c, u, 0))
            par[u] = v_left
    for u, c in adj[v_right]:
        if not seen[u]:
            heappush(pq, (-c, u, 1))
            par[u] = v_right
    ans = ST.query(1, N)
    mi = 10 ** 10
    while pq:
        c, v, flg = heappop(pq)
        c *= -1
        if flg == 0:
            if par[v] != v_left:
                continue
            i = E[v_left * (N + 1) + v]
        else:
            if par[v] != v_right:
                continue
            i = E[v_right * (N + 1) + v]
        mi = min(mi, c)
        ST.update(i, 0)
        ans = min(ans, max(ST.query(1, N), ma - mi))
        seen[v] = 1
        if flg == 0:
            v_left = v
            for u, cc in adj[v]:
                if not seen[u]:
                    heappush(pq, (-cc, u, 0))
                    par[u] = v
        else:
            v_right = v
            for u, cc in adj[v]:
                if not seen[u]:
                    heappush(pq, (-cc, u, 1))
                    par[u] = v
    print(ans)


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