結果

問題 No.3113 The farthest point
ユーザー kq5y
提出日時 2025-04-19 16:00:24
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 703 bytes
コンパイル時間 512 ms
コンパイル使用メモリ 12,160 KB
実行使用メモリ 90,116 KB
最終ジャッジ日時 2025-04-19 16:00:57
合計ジャッジ時間 30,802 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque

# bfs to bfs で最長距離を求める

n = int(input())
graph = defaultdict(list)
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    graph[u].append((v, w))
    graph[v].append((u, w))


def bfs(start):
    dist = [-1] * (n + 1)
    dist[start] = 0
    queue = deque([start])
    while queue:
        node = queue.popleft()
        for neighbor, weight in graph[node]:
            if dist[neighbor] == -1:
                dist[neighbor] = dist[node] + weight
                queue.append(neighbor)
    farthest_node = dist.index(max(dist))
    return farthest_node, dist[farthest_node]


u, _ = bfs(1)
_, diameter = bfs(u)

print(diameter)
0