728x90
https://www.acmicpc.net/problem/1967
import sys
from collections import defaultdict
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
graph = defaultdict(list)
def bfs(node, dist):
for next_node, next_dist in graph[node]:
if visited[next_node] == -1:
visited[next_node] = dist + next_dist
bfs(next_node, dist + next_dist)
return max(visited)
N = int(input())
for i in range(N - 1):
u, w, v = map(int, input().rstrip().split(" "))
graph[u].append((w, v))
graph[w].append((u, v))
visited = [-1] * (N + 1)
visited[1] = 0
answer = bfs(1, 0)
A = visited.index(answer)
visited = [-1] * ( N + 1)
visited[A] = 0
answer = bfs(A, 0)
print(answer)
눈물나는 시도들...
728x90
'Study > Coding Test' 카테고리의 다른 글
[백준] 1431 - 시리얼 번호 Python (0) | 2023.08.14 |
---|---|
[백준] 21608 - 상어 초등학교 Python (0) | 2023.08.11 |
[프로그래머스] 가장 큰 수 Python (0) | 2023.08.09 |
[프로그래머스] 등굣길 Python (0) | 2023.08.09 |
[프로그래머스] C++ 문자열 다루기 관련 문제들 (0) | 2023.08.02 |