結果
| 問題 | 
                            No.3113 The farthest point
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2025-04-18 23:27:18 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 999 ms / 2,000 ms | 
| コード長 | 1,700 bytes | 
| コンパイル時間 | 481 ms | 
| コンパイル使用メモリ | 12,032 KB | 
| 実行使用メモリ | 112,516 KB | 
| 最終ジャッジ日時 | 2025-04-18 23:27:36 | 
| 合計ジャッジ時間 | 17,323 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 33 | 
ソースコード
import sys
sys.setrecursionlimit(1 << 25)
def main():
    input = sys.stdin.read
    data = input().split()
    idx = 0
    N = int(data[idx])
    idx += 1
    adj = [[] for _ in range(N + 1)]
    for _ in range(N - 1):
        u = int(data[idx])
        v = int(data[idx+1])
        w = int(data[idx+2])
        adj[u].append((v, w))
        adj[v].append((u, w))
        idx += 3
    max_depth = [0] * (N + 1)
    max_path = [0] * (N + 1)
    ans = 0
    stack = [(1, None, False)]
    while stack:
        u, parent, processed = stack.pop()
        if not processed:
            stack.append((u, parent, True))
            for v, w in adj[u]:
                if v != parent:
                    stack.append((v, u, False))
        else:
            child_depths = []
            current_max_path = 0
            for v, w in adj[u]:
                if v == parent:
                    continue
                child_depth = max_depth[v] + w
                if child_depth > 0:
                    child_depths.append(child_depth)
                if max_path[v] > current_max_path:
                    current_max_path = max_path[v]
            child_depths.sort(reverse=True)
            sum_top_two = 0
            if len(child_depths) >= 1:
                sum_top_two = child_depths[0]
                if len(child_depths) >= 2:
                    sum_top_two += child_depths[1]
            current_max_path = max(current_max_path, sum_top_two)
            ans = max(ans, current_max_path)
            max_depth_u = child_depths[0] if child_depths else 0
            max_depth[u] = max_depth_u
            max_path[u] = current_max_path
    print(ans)
if __name__ == "__main__":
    main()