結果
| 問題 | 
                            No.3113 The farthest point
                             | 
                    
| コンテスト | |
| ユーザー | 
                             norioc
                         | 
                    
| 提出日時 | 2025-04-19 07:39:25 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 672 ms / 2,000 ms | 
| コード長 | 539 bytes | 
| コンパイル時間 | 586 ms | 
| コンパイル使用メモリ | 82,552 KB | 
| 実行使用メモリ | 124,736 KB | 
| 最終ジャッジ日時 | 2025-04-19 07:39:38 | 
| 合計ジャッジ時間 | 13,059 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 33 | 
ソースコード
from collections import defaultdict, deque
N = int(input())
adj = defaultdict(list)
for _ in range(N-1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    adj[u].append((v, w))
    adj[v].append((u, w))
def tree_dp(v, par):
    global ans
    res = [0]
    for to, w in adj[v]:
        if to == par: continue
        d = tree_dp(to, v) + w
        if d > 0:
            res.append(d)
    res.sort()
    if len(res) > 1:
        ans = max(ans, res[-1] + res[-2])
    return res[-1]
ans = 0
tree_dp(0, -1)
print(ans)
            
            
            
        
            
norioc