백준 1967 - 트리의 지름 (파이썬)
Updated:
Answer
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
V = int(input())
tree = [[] for _ in range(V + 1)]
for _ in range(V - 1):
a, b, c = map(int, input().split())
tree[a].append((b, c))
tree[b].append((a, c))
def dfs(start):
for next, cost in tree[start]:
if path[next] == 0:
path[next] = path[start] + cost
dfs(next)
path = [0] * (V + 1)
dfs(1)
path[1] = 0
index = 0
tmp = 0
for i in range(V + 1):
if tmp < path[i]:
tmp = path[i]
index = i
path = [0] * (V + 1)
dfs(index)
path[index] = 0
print(max(path))