結果
| 問題 | No.3113 The farthest point |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-19 22:37:33 |
| 言語 | Python3 (3.15.0 + numpy 2.5.4 + scipy 1.18.1 + ACL) |
| 結果 |
WJ
不安定
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 697 bytes |
| 記録 | |
| 最終ジャッジ日時 | 2026-10-17 00:27:28 |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge5_0 |
(要ログイン)
ソースコード
import sys
sys.setrecursionlimit(1 << 25)
N = int(input())
from collections import defaultdict
tree = defaultdict(list)
for _ in range(N - 1):
u, v, w = map(int, input().split())
tree[u].append((v, w))
tree[v].append((u, w))
max_path = 0
def dfs(node, parent):
global max_path
best1, best2 = 0, 0
for neighbor, weight in tree[node]:
if neighbor == parent:
continue
gain = dfs(neighbor, node) + weight
gain = max(0, gain)
if gain > best1:
best1, best2 = gain, best1
elif gain > best2:
best2 = gain
max_path = max(max_path, best1 + best2)
return best1
dfs(1, -1)
print(max_path)