結果

問題 No.1833 Subway Planning
ユーザー mkawa2mkawa2
提出日時 2022-02-05 18:59:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,612 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 404,252 KB
最終ジャッジ日時 2024-06-11 13:39:01
合計ジャッジ時間 22,954 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,656 KB
testcase_01 AC 45 ms
54,272 KB
testcase_02 AC 46 ms
54,400 KB
testcase_03 AC 44 ms
54,528 KB
testcase_04 AC 742 ms
267,708 KB
testcase_05 AC 2,179 ms
404,252 KB
testcase_06 AC 912 ms
174,328 KB
testcase_07 AC 1,024 ms
181,240 KB
testcase_08 AC 1,050 ms
177,660 KB
testcase_09 WA -
testcase_10 AC 783 ms
165,788 KB
testcase_11 AC 519 ms
172,800 KB
testcase_12 AC 453 ms
208,272 KB
testcase_13 AC 794 ms
162,968 KB
testcase_14 AC 698 ms
138,676 KB
testcase_15 AC 447 ms
164,116 KB
testcase_16 AC 742 ms
138,228 KB
testcase_17 AC 913 ms
139,520 KB
testcase_18 AC 864 ms
133,560 KB
testcase_19 AC 507 ms
136,852 KB
testcase_20 AC 375 ms
121,928 KB
testcase_21 AC 3,319 ms
338,224 KB
testcase_22 AC 2,187 ms
267,160 KB
testcase_23 AC 42 ms
54,784 KB
testcase_24 AC 44 ms
54,528 KB
testcase_25 AC 42 ms
54,400 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

n = II()
to = [[] for _ in range(n)]
cc = [0]
ci = [(0, 0)]
for i in range(1, n):
    u, v, c = LI1()
    to[u].append((v, i))
    to[v].append((u, i))
    cc.append(c+1)
    ci.append((c+1, i))
ci.sort()
mx = ci[-1][0]
# pDB(ci)

ep = [-1]*n
ed = [0]*n
st = [(0, 0)]
while st:
    u, pi = st.pop()
    for v, i in to[u]:
        if i == pi: continue
        ep[i] = pi
        ed[i] = ed[pi]+1
        st.append((v, i))
# pDB(ep, ed)

def binary_search(l, r, ok, minimize):
    if minimize: l -= 1
    else: r += 1
    while l+1 < r:
        m = (l+r)//2
        if ok(m) ^ minimize: l = m
        else: r = m
    if minimize: return r
    return l

from bisect import bisect_right
from collections import Counter

def ok(m):
    si = bisect_right(ci, (m, n+1))
    use = set()
    di = []
    dcnt = Counter()
    for _, i in ci[si:]:
        use.add(i)
        d = ed[i]
        di.append((d, i))
        if dcnt[d] == 2: return False
        dcnt[d] += 1

    di.sort(key=lambda x: -x[0])
    if len(di) > 2 and di[1][0] == di[2][0]:
        if di[0][0] != di[1][0]+1: return False
        i, j = di[0][1], di[2][1]
        if ep[i] != j: di[1], di[2] = di[2], di[1]
        j = di[2][1]
        if ep[i] != j: return False

    d0, i = di[0]
    d1, j = di[1]
    mn = cc[i]

    def move(i, mn):
        if i in use: use.remove(i)
        i = ep[i]
        mn = min(mn, cc[i])
        return i, mn

    for _ in range(d0-d1):
        i, mn = move(i, mn)
        if mx-mn > m: return False

    if i == j:
        a = di[-1][1]
        while i != a:
            i, mn = move(i, mn)
            if mx-mn > m: return False
        use.remove(a)
    else:
        while 1:
            i, mn = move(i, mn)
            j, mn = move(j, mn)
            if i==j:break
            if mx-mn > m: return False

    return len(use) == 0

ans = binary_search(0, ci[-2][0], ok, True)
print(ans)
0