https://www.acmicpc.net/problem/1967
#1967_트리의 지름
from collections import deque
n = int(input())
tree = [[] for _ in range(n+1)]
for _ in range(1,n):
parent, child, weight = map(int, input().split())
tree[parent].append([weight, child])
tree[child].append([weight, parent])
def bfs(node):
queue=deque()
queue.append([0,node])
visited=[0]*n
visited[node-1]=1
diameter=[0,0]
while queue:
cnt, cur = queue.popleft()
for i in tree[cur]:
wei_val, nextt = i[0], i[1]
if visited[nextt-1]==0:
visited[nextt-1]=1
queue.append((cnt+wei_val, nextt))
if diameter[0] < cnt+wei_val:
diameter[1] = nextt
diameter[0] = cnt+wei_val
return diameter
parent = bfs(1)
child = bfs(parent[1])
print(child[0])
'알고리즘 > 백준(BOJ)' 카테고리의 다른 글
[백준/BOJ]#1780: 종이의 개수 [분할정복/재귀/파이썬/python] (0) | 2022.01.13 |
---|---|
[백준/BOJ]#17413: 단어 뒤집기 2 [문자열/파이썬/python] (0) | 2021.11.22 |
[백준/알고리즘] #5554:심부름 가는 길[파이썬(python)/수학] (0) | 2021.11.09 |
[백준/알고리즘] #9663:N-Queen[파이썬(python)/백트래킹] (0) | 2021.11.08 |
[백준/알고리즘] #16212:정열적인 정렬 [파이썬(python)/정렬] (0) | 2021.10.20 |