結果

問題 No.1833 Subway Planning
ユーザー mkawa2mkawa2
提出日時 2022-02-06 22:29:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,065 ms / 4,000 ms
コード長 2,965 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 87,256 KB
実行使用メモリ 207,228 KB
最終ジャッジ日時 2023-09-02 07:35:50
合計ジャッジ時間 18,686 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,240 KB
testcase_01 AC 77 ms
71,084 KB
testcase_02 AC 79 ms
71,168 KB
testcase_03 AC 78 ms
71,232 KB
testcase_04 AC 650 ms
162,528 KB
testcase_05 AC 712 ms
162,704 KB
testcase_06 AC 753 ms
161,012 KB
testcase_07 AC 951 ms
166,148 KB
testcase_08 AC 929 ms
166,504 KB
testcase_09 AC 653 ms
147,880 KB
testcase_10 AC 904 ms
207,228 KB
testcase_11 AC 864 ms
153,852 KB
testcase_12 AC 656 ms
151,740 KB
testcase_13 AC 820 ms
205,232 KB
testcase_14 AC 679 ms
189,620 KB
testcase_15 AC 680 ms
127,252 KB
testcase_16 AC 828 ms
145,684 KB
testcase_17 AC 1,065 ms
187,440 KB
testcase_18 AC 1,023 ms
179,424 KB
testcase_19 AC 772 ms
138,396 KB
testcase_20 AC 675 ms
126,872 KB
testcase_21 AC 1,051 ms
173,548 KB
testcase_22 AC 1,020 ms
165,936 KB
testcase_23 AC 76 ms
71,032 KB
testcase_24 AC 78 ms
71,244 KB
testcase_25 AC 77 ms
70,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = (1 << 63)-1
inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

from heapq import *

class RemovableHeap:
    def __init__(self):
        self.hp = []
        self.rem = []
        self.cnt = {}
        self.size = 0
        self.s = 0

    def __len__(self): return self.size

    def __getitem__(self, i):
        assert i == 0
        self.__trim()
        return self.hp[0]

    def push(self, val):
        heappush(self.hp, val)
        self.s += val
        if val in self.cnt: self.cnt[val] += 1
        else: self.cnt[val] = 1
        self.size += 1
        return True

    def pop(self):
        self.__trim()
        assert self.hp
        self.size -= 1
        val = heappop(self.hp)
        self.s -= val
        if self.cnt[val] == 1: del self.cnt[val]
        else: self.cnt[val] -= 1
        return val

    def remove(self, val):
        if val not in self.cnt: return False
        self.size -= 1
        self.s -= val
        if self.cnt[val] == 1: del self.cnt[val]
        else: self.cnt[val] -= 1
        heappush(self.rem, val)
        return True

    def __trim(self):
        while self.rem and self.rem[0] == self.hp[0]:
            heappop(self.hp)
            heappop(self.rem)

def dfs(root):
    hp = RemovableHeap()
    # hp.push(inf)
    st = [root]
    while st:
        u = st.pop()
        for v, c in to[u]:
            if v == par[u]: continue
            par[v] = u
            cc[v] = c
            mns[v] = min(mns[u], c)
            hp.push(-c)
            st.append(v)
    if len(hp) == 0: return 0
    ans = -hp[0]
    st = [(root, 1)]
    while st:
        u, f = st.pop()
        if f:
            if u != root:
                hp.remove(-cc[u])
                if len(hp): ans = min(ans, max(-hp[0], mx-mns[u]))
                else: ans = min(ans, mx-mns[u])
            st.append((u, 0))
            for v, c in to[u]:
                if v == par[u]: continue
                st.append((v, 1))
        else:
            hp.push(-cc[u])
    return ans

n = II()
to = [[] for _ in range(n)]
mx = -1
ru = rv = 0
for i in range(1, n):
    u, v, c = LI1()
    c += 1
    to[u].append((v, c))
    to[v].append((u, c))
    if c > mx: mx, ru, rv = c, u, v

par = [-1]*n
cc = [0]*n
par[ru] = rv
par[rv] = ru
mns = [inf]*n
ans = max(dfs(ru), dfs(rv))
print(ans)
0