結果
| 問題 | No.3113 The farthest point |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-19 02:34:44 |
| 言語 | Python3 (3.15.0 + numpy 2.5.4 + scipy 1.18.1 + ACL) |
| 結果 |
WJ
不安定
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,981 bytes |
| 記録 | |
| 最終ジャッジ日時 | 2026-10-17 00:18:14 |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge4_0 |
(要ログイン)
ソースコード
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()