結果

問題 No.3113 The farthest point
ユーザー K2
提出日時 2025-04-19 03:17:50
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 462 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 82,328 KB
実行使用メモリ 120,916 KB
最終ジャッジ日時 2025-04-19 03:18:07
合計ジャッジ時間 14,482 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

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

dist = [0] * N
def dfs(v, p):
    for u, w in G[v]:
        if u == p:
            continue
        dist[u] = dist[v] + w
        dfs(u, v)
dfs(0, -1)

leaf = [i for i in range(N) if len(G[i]) == 1]
leaf.sort(key=lambda x: dist[x], reverse=True)
v = leaf[0]
dist = [0] * N
dfs(v, -1)
print(max(dist))
0