結果

問題 No.1833 Subway Planning
ユーザー ああいいああいい
提出日時 2022-02-05 12:30:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 954 ms / 4,000 ms
コード長 1,583 bytes
コンパイル時間 378 ms
コンパイル使用メモリ 87,224 KB
実行使用メモリ 169,868 KB
最終ジャッジ日時 2023-09-02 06:34:18
合計ジャッジ時間 15,828 ms
ジャッジサーバーID
(参考情報)
judge16 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,328 KB
testcase_01 AC 68 ms
71,348 KB
testcase_02 AC 68 ms
71,580 KB
testcase_03 AC 70 ms
71,220 KB
testcase_04 AC 372 ms
144,272 KB
testcase_05 AC 387 ms
147,052 KB
testcase_06 AC 639 ms
147,156 KB
testcase_07 AC 770 ms
147,192 KB
testcase_08 AC 779 ms
147,244 KB
testcase_09 AC 533 ms
140,768 KB
testcase_10 AC 771 ms
169,692 KB
testcase_11 AC 719 ms
166,600 KB
testcase_12 AC 523 ms
165,096 KB
testcase_13 AC 776 ms
169,868 KB
testcase_14 AC 630 ms
145,020 KB
testcase_15 AC 583 ms
146,952 KB
testcase_16 AC 786 ms
148,636 KB
testcase_17 AC 954 ms
150,520 KB
testcase_18 AC 872 ms
142,244 KB
testcase_19 AC 697 ms
147,776 KB
testcase_20 AC 538 ms
126,804 KB
testcase_21 AC 799 ms
152,644 KB
testcase_22 AC 777 ms
155,384 KB
testcase_23 AC 70 ms
71,556 KB
testcase_24 AC 70 ms
71,204 KB
testcase_25 AC 69 ms
71,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())

G = [[] for _ in range(N+1)]
edge = []
for _ in range(N-1):
    a,b,c = map(int,input().split())
    G[a].append((b,c))
    G[b].append((a,c))
    edge.append((c,a,b))
edge.sort(reverse = True,key = lambda x:x[0])
M,u,v = edge[0]
_min = edge[0][0]
import sys
sys.setrecursionlimit(10 ** 8)
parent = [0] * (N+1)
parent[u] = (u,M)
parent[v] = (v,M)
"""
def dfs(now,p):
    for v,c in G[now]:
        if v == p:continue
        parent[v] = (now,c)
        dfs(v,now)
"""
def dfs(now,p):
    stack = [(now,p)]
    while stack:
        now,p = stack.pop()
        for v,c in G[now]:
            if v == p:continue
            parent[v] = (now,c)
            stack.append((v,now))
dfs(u,v)
dfs(v,u)
memo = [0] * (N+1)
memo[u] = 1
memo[v] = 1
for i in range(1,N-1):
    c,a,b = edge[i]
    if memo[a] == 1 and memo[b] == 1:
        continue
    tmp = _min
    if parent[b][0] == a:
        a,b = b,a
    d = a
    while memo[a] == 0:
        memo[a] = 1
        if parent[a][1] < tmp:
            tmp = parent[a][1]
        a = parent[a][0]
    if a != u and a != v:
        break
    if M - tmp > c:
        break
    if u == a:
        u = d
    else:
        v = d
    _min = tmp
while parent[v][0] != v:
    parent[v] = (parent[v][0],parent[v][1] - _min)
    v = parent[v][0]
while parent[u][0] != u:
    parent[u] = (parent[u][0],parent[u][1] - _min)
    u = parent[u][0]
parent[u] = (parent[u][0],parent[u][1] - _min)
parent[v] = (parent[v][0],parent[v][1] - _min)
ans = 0
for i in range(1,N+1):
    if parent[i][1] > ans:
        ans = parent[i][1]
print(ans)
    
0