문제
문제 바로가기
해결
import sys
sys.setrecursionlimit(10 ** 9)
def dfs(s, sw):
for n, nw in graph[s]:
if visited[n] == -1:
visited[n] = sw + nw
dfs(n, sw + nw)
n = int(input())
graph = [[] for _ in range(n + 1)]
visited = [-1] * (n + 1)
for _ in range(n-1):
a, b, cost = map(int, input().split())
graph[a].append((b, cost))
graph[b].append((a, cost))
start = visited.index(max(visited))
visited = [-1] * (n + 1)
visited[1] = 0
dfs(1, 0)
start = visited.index(max(visited))
visited = [-1] * (n + 1)
visited[start] = 0
dfs(start, 0)
print(max(visited))
댓글남기기