結果
問題 |
No.3113 The farthest point
|
ユーザー |
|
提出日時 | 2025-04-19 13:06:10 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 835 bytes |
コンパイル時間 | 275 ms |
コンパイル使用メモリ | 12,288 KB |
実行使用メモリ | 80,980 KB |
最終ジャッジ日時 | 2025-04-19 13:06:40 |
合計ジャッジ時間 | 25,830 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 20 WA * 13 |
ソースコード
# 重み付き木の直径を求める N = int(input()) edges = [[] for _ in range(N)] for _ in range(N - 1): a, b, c = map(int, input().split()) edges[a - 1].append((b - 1, c)) edges[b - 1].append((a - 1, c)) def dfs(start, edges): dist = [-1] * len(edges) dist[start] = 0 stack = [start] max_dist = 0 max_node = start while stack: node = stack.pop() for neighbor, weight in edges[node]: if dist[neighbor] == -1: dist[neighbor] = dist[node] + weight stack.append(neighbor) if dist[neighbor] > max_dist: max_dist = dist[neighbor] max_node = neighbor return max_node, max_dist farthest_node, _ = dfs(0, edges) _, diameter = dfs(farthest_node, edges) print(diameter)