結果

問題 No.1833 Subway Planning
ユーザー 👑 tamatotamato
提出日時 2022-02-04 23:34:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,729 ms / 4,000 ms
コード長 4,993 bytes
コンパイル時間 545 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 256,456 KB
最終ジャッジ日時 2023-09-02 06:06:08
合計ジャッジ時間 26,411 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
71,956 KB
testcase_01 AC 96 ms
71,752 KB
testcase_02 AC 96 ms
71,996 KB
testcase_03 AC 96 ms
72,088 KB
testcase_04 AC 861 ms
240,356 KB
testcase_05 AC 811 ms
255,540 KB
testcase_06 AC 1,063 ms
240,796 KB
testcase_07 AC 1,269 ms
241,348 KB
testcase_08 AC 1,302 ms
248,672 KB
testcase_09 AC 994 ms
252,328 KB
testcase_10 AC 1,460 ms
252,784 KB
testcase_11 AC 1,028 ms
254,696 KB
testcase_12 AC 968 ms
254,156 KB
testcase_13 AC 1,492 ms
256,456 KB
testcase_14 AC 1,218 ms
246,872 KB
testcase_15 AC 999 ms
241,308 KB
testcase_16 AC 1,312 ms
248,312 KB
testcase_17 AC 1,729 ms
249,004 KB
testcase_18 AC 1,613 ms
239,876 KB
testcase_19 AC 1,256 ms
246,336 KB
testcase_20 AC 1,013 ms
207,556 KB
testcase_21 AC 1,362 ms
254,144 KB
testcase_22 AC 1,380 ms
241,860 KB
testcase_23 AC 97 ms
71,816 KB
testcase_24 AC 98 ms
71,700 KB
testcase_25 AC 98 ms
71,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


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

    # adj[0] must be empty list
    def EulerTour(adj, root, v_comp):
        st = [root]
        ret = []
        seen = [0] * len(adj)
        par = [0] * len(adj)
        depth = [0] * len(adj)
        while st:
            v = st.pop()
            if seen[v]:
                ret.append(v)
                continue
            ret.append(v)
            seen[v] = 1
            if par[v] != 0:
                st.append(par[v])
            for u, c in adj[v]:
                if u == v_comp:
                    continue
                if seen[u] == 0:
                    st.append(u)
                    par[u] = v
                    depth[u] = depth[v] + 1

        return ret, depth

    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]

    INFO = []
    for v0 in [v_left, v_right]:
        info = []
        v_comp = v_left + v_right - v0
        et, depth = EulerTour(adj, v0, v_comp)
        mi_val = [ma] * (N + 1)
        ST_list = [-1] * N
        for j in range(len(et) - 1):
            v = et[j]
            u = et[j + 1]
            i = E[v * (N + 1) + u]
            c = CC[i - 1]
            ST_list[i - 1] = c
        ST = SegmentTree(ST_list)
        info.append((ma, ST.query(1, N)))
        for j in range(len(et) - 1):
            v = et[j]
            u = et[j + 1]
            i = E[v * (N + 1) + u]
            c = CC[i - 1]
            if depth[u] > depth[v]:
                ST.update(i, 0)
                mi_val[u] = min(c, mi_val[v])
                info.append((mi_val[u], ST.query(1, N)))
            else:
                ST.update(i, c)
        INFO.append(info)

    info0, info1 = INFO
    info0.sort(key=lambda x: x[0])
    info1.sort(key=lambda x: x[0])
    info0 = [(-1, 10 ** 9 + 1)] + info0
    info1 = [(-1, 10 ** 9 + 1)] + info1
    ans = 10 ** 9 + 1
    d0 = d1 = 10 ** 9 + 1
    #print(info0, info1)
    while len(info0) > 1 or len(info1) > 1:
        m0 = info0[-1][0]
        m1 = info1[-1][0]
        if m0 > m1:
            m, d = info0.pop()
            d0 = min(d0, d)
        else:
            m, d = info1.pop()
            d1 = min(d1, d)
        ans = min(ans, max(ma - m, max(d0, d1)))
    print(ans)



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