結果
| 問題 | 
                            No.3113 The farthest point
                             | 
                    
| コンテスト | |
| ユーザー | 
                             titia
                         | 
                    
| 提出日時 | 2025-04-20 01:25:22 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 643 ms / 2,000 ms | 
| コード長 | 969 bytes | 
| コンパイル時間 | 318 ms | 
| コンパイル使用メモリ | 82,336 KB | 
| 実行使用メモリ | 140,832 KB | 
| 最終ジャッジ日時 | 2025-04-20 01:25:35 | 
| 合計ジャッジ時間 | 12,503 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 33 | 
ソースコード
import sys
input = sys.stdin.readline
N=int(input())
E=[[] for i in range(N)]
for i in range(N-1):
    x,y,w=map(int,input().split())
    x-=1
    y-=1
    E[x].append((y,w))
    E[y].append((x,w))
ROOT=0
QUE=[ROOT] 
Parent=[-1]*N
Parent[ROOT]=N # ROOTの親を定めておく.
Child=[[] for i in range(N)]
TOP_SORT=[] # トポロジカルソート
while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to,w in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            Child[x].append(to)
            QUE.append(to)
DP=[-1<<63]*N
ANS=0
for x in TOP_SORT[::-1]:
    LIST=[]
    for to,w in E[x]:
        if to==Parent[x]:
            continue
        LIST.append(DP[to]+w)
    LIST.sort(reverse=True)
    if LIST:
        ANS=max(ANS,LIST[0])
    if len(LIST)>=2:
        ANS=max(ANS,LIST[0]+LIST[1])
    if LIST:
        DP[x]=max(0,LIST[0])
    else:
        DP[x]=0
print(ANS)
            
            
            
        
            
titia