結果
問題 |
No.3113 The farthest point
|
ユーザー |
|
提出日時 | 2025-04-19 02:34:44 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 1,262 ms / 2,000 ms |
コード長 | 1,981 bytes |
コンパイル時間 | 476 ms |
コンパイル使用メモリ | 12,288 KB |
実行使用メモリ | 116,164 KB |
最終ジャッジ日時 | 2025-04-19 02:35:06 |
合計ジャッジ時間 | 21,933 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
import sys from collections import deque sys.setrecursionlimit(1 << 25) def main(): input = sys.stdin.read().split() idx = 0 n = int(input[idx]) idx += 1 adj = [[] for _ in range(n + 1)] for _ in range(n - 1): u = int(input[idx]) v = int(input[idx + 1]) w = int(input[idx + 2]) idx += 3 adj[u].append((v, w)) adj[v].append((u, w)) max_in_subtree = [-float('inf')] * (n + 1) max_single = [0] * (n + 1) stack = [(1, -1, False)] while stack: u, parent, visited = stack.pop() if not visited: stack.append((u, parent, True)) for v, w in adj[u]: if v != parent: stack.append((v, u, False)) else: max1 = -float('inf') max2 = -float('inf') current_max_in_subtree = -float('inf') for v, w in adj[u]: if v == parent: continue current_child_in = max_in_subtree[v] current_child_single = max_single[v] if current_child_in > current_max_in_subtree: current_max_in_subtree = current_child_in current_single = current_child_single + w if current_single > max1: max2 = max1 max1 = current_single elif current_single > max2: max2 = current_single current_max = -float('inf') if max1 != -float('inf') and max2 != -float('inf'): current_max = max1 + max2 if max1 != -float('inf'): current_max = max(current_max, max1) max_in_subtree[u] = max(current_max_in_subtree, current_max) max_single_val = max(0, max1) if max1 != -float('inf') else 0 max_single[u] = max_single_val print(max(max_in_subtree[1], 0)) if __name__ == '__main__': main()