結果

問題 No.3113 The farthest point
コンテスト
ユーザー kq5y
提出日時 2025-04-20 17:48:37
言語 Python3
(3.15.0 + numpy 2.5.4 + scipy 1.18.1 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WJ  
(最新)
AC  
(最初)
実行時間 -
コード長 892 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
最終ジャッジ日時 2026-10-17 00:31:29
ジャッジサーバーID
(参考情報)
judge4_0 / judge3_0
このコードへのチャレンジ
(要ログイン)

ソースコード

diff #
raw source code

from collections import defaultdict

n = int(input())
graph = defaultdict(list)
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    graph[u].append((v, w))
    graph[v].append((u, w))

parent = [0] * (n + 1)
order = []
stack = [1]
parent[1] = -1
while stack:
    u = stack.pop()
    order.append(u)
    for v, w in graph[u]:
        if parent[v] == 0:
            parent[v] = u
            stack.append(v)

best_down = [0] * (n + 1)
answer = 0

for u in reversed(order):
    first = 0
    second = 0
    for v, w in graph[u]:
        if v == parent[u]:
            continue
        c = best_down[v] + w
        if c > first:
            second = first
            first = c
        elif c > second:
            second = c
    if first < 0:
        first = 0
    if second < 0:
        second = 0
    answer = max(answer, first + second)
    best_down[u] = first

print(answer)
0