結果

問題 No.1833 Subway Planning
ユーザー puznekopuzneko
提出日時 2022-03-04 01:34:08
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,153 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 86,956 KB
実行使用メモリ 448,288 KB
最終ジャッジ日時 2023-09-24 21:46:21
合計ジャッジ時間 42,646 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,504 KB
testcase_01 AC 75 ms
71,092 KB
testcase_02 AC 75 ms
71,540 KB
testcase_03 AC 73 ms
71,284 KB
testcase_04 AC 1,512 ms
304,912 KB
testcase_05 AC 2,491 ms
391,348 KB
testcase_06 AC 1,764 ms
370,112 KB
testcase_07 AC 3,648 ms
373,656 KB
testcase_08 AC 2,963 ms
364,256 KB
testcase_09 TLE -
testcase_10 AC 2,562 ms
356,080 KB
testcase_11 AC 1,797 ms
429,036 KB
testcase_12 AC 1,750 ms
440,600 KB
testcase_13 AC 1,988 ms
354,072 KB
testcase_14 AC 560 ms
214,696 KB
testcase_15 AC 1,486 ms
260,424 KB
testcase_16 AC 561 ms
209,196 KB
testcase_17 AC 1,367 ms
205,440 KB
testcase_18 AC 1,086 ms
207,164 KB
testcase_19 AC 1,397 ms
179,588 KB
testcase_20 AC 1,681 ms
167,456 KB
testcase_21 AC 2,796 ms
325,664 KB
testcase_22 AC 2,405 ms
307,528 KB
testcase_23 AC 73 ms
70,992 KB
testcase_24 AC 72 ms
71,280 KB
testcase_25 AC 74 ms
71,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
n, *indata = map(int, stdin.read().split())
offset = 0
a = []
b = []
c = []
g = [[] for i in range(n+1)]
for i in range(n-1):
    s, t, d  = indata[offset + 3*i]-1,indata[offset + 3*i+1]-1,indata[offset + 3*i+2]
    a.append(s)
    b.append(t)
    c.append(d)
    g[s].append((t,d))
    g[t].append((s,d))

left = -1
right = max(c) + 2
while right - left >= 2:
    mid = (right + left) // 2
    mustin = set()
    start = -1
    for i in range(n-1):
        if c[i] > mid:
            mustin.add(a[i])
            mustin.add(b[i])
            start = i
    if start == -1:
        right = mid
        continue
    mustvisit = len(mustin)
    visited = 0
    used = [c[start]]
    check = [False for i in range(n)]
    check[a[start]] = True
    check[b[start]] = True
    que = [(a[start],0,-1)]
    while que:
        now, inout, come = que.pop()
        if inout == 0:
            check[now] = True
            if come != -1:
                used.append(come)
            if now in mustin:
                visited += 1
                if visited == mustvisit:
                    break
            que.append((now,1,come))
            for i, cost in g[now]:
                if not check[i]:
                    que.append((i,0,cost))
        else:
            if now in mustin:
                break
            else:
                used.pop()
    que = [(b[start],0,-1)]
    while que:
        now, inout, come = que.pop()
        if inout == 0:
            check[now] = True
            if come != -1:
                used.append(come)
            if now in mustin:
                visited += 1
                if visited == mustvisit:
                    break
            que.append((now,1,come))
            for i, cost in g[now]:
                if not check[i]:
                    que.append((i,0,cost))
        else:
            if now in mustin:
                break
            else:
                used.pop()
    if visited != mustvisit:
        left = mid
    else:
        val = max(used) - min(used)
        if val <= mid:
            right = mid
        else:
            left = mid

print("{}".format(right))
0