BOJ - 1967 - 트리의 지름
Updated:
import sys
from collections import deque
def BFS(graph, x, mode, N):
Q = deque()
Q.append(x)
check = [-1 for _ in range(N)]
check[x] = 0
while Q:
x = Q.popleft()
for nx, weight in graph[x]:
if check[nx] == -1:
check[nx] = check[x] + weight
Q.append(nx)
if mode:
return check.index(max(check))
else:
return max(check)
def solution():
N = int(input())
graph = [[] for _ in range(N)]
for _ in range(N-1):
x, y, w = map(int, sys.stdin.readline().split())
graph[x-1].append((y-1, w))
graph[y-1].append((x-1, w))
print(BFS(graph, BFS(graph, 0, True, N), False, N))
solution()
https://www.acmicpc.net/problem/1967
Leave a comment