結果

問題 No.3113 The farthest point
ユーザー Koi
提出日時 2025-04-18 23:15:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 497 ms / 2,000 ms
コード長 1,012 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 113,920 KB
最終ジャッジ日時 2025-04-18 23:15:49
合計ジャッジ時間 10,088 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append((v, w))
    G[v].append((u, w))

done = [0] * N
Q = [~0, 0] # 根をスタックに追加
dp = ["?"] * N
ET = []
ans = 0
while Q:
    i = Q.pop()
    if i >= 0: # 行きがけの処理
        done[i] = 1
        # ET.append(i)
        for (a, w) in G[i]:
            if done[a]: continue
            Q.append(~a) # 帰りがけの処理をスタックに追加
            Q.append(a) # 行きがけの処理をスタックに追加
    
    else: # 帰りがけの処理
        i = ~i
        L = []
        for (x, w) in G[i]:
            if(dp[x] != "?"):
                L.append(dp[x] + w)
        L.sort(reverse=True)
        # print(i, L)
        if(len(L) >= 1):
            dp[i] = max(0, L[0])
            ans = max(ans, L[0])
        else:
            dp[i] = 0
        if(len(L) >= 2):
            ans = max(ans, sum(L[:2]))
        
        
print(ans)
0