結果

問題 No.3113 The farthest point
ユーザー Rira
提出日時 2025-04-20 18:21:46
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,163 ms / 2,000 ms
コード長 862 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 80,968 KB
最終ジャッジ日時 2025-04-20 18:22:07
合計ジャッジ時間 19,289 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(2 * 10 ** 5 + 10)
input = sys.stdin.readline

N = int(input())
from collections import defaultdict

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))

max_diameter = 0

def dfs(u, parent):
    global max_diameter
    top1 = 0  # 最大の枝の長さ
    top2 = 0  # 2番目に大きい枝の長さ

    for v, w in graph[u]:
        if v == parent:
            continue
        child = dfs(v, u) + w
        if child < 0:
            child = 0
        if child > top1:
            top2 = top1
            top1 = child
        elif child > top2:
            top2 = child

    # このノードを経由するパスの最大長を更新
    max_diameter = max(max_diameter, top1 + top2)
    return top1

dfs(1, -1)
print(max_diameter)
0