https://programmers.co.kr/learn/courses/30/lessons/49189
전형적인 bfs 문제이다.
그래프 2차원 배열을 만들고 방문배열, 거리배열을 만들었다.
from collections import deque
def solution(n, edge):
answer = 0
max_distance = 0
visited = [0] * (n+1)
distance = [0] * (n+1)
graph = [[] for i in range(n+1)]
for e in edge:
graph[e[0]].append(e[1])
graph[e[1]].append(e[0])
q = deque([1])
while q:
v = q.popleft()
visited[v] = 1
for x in graph[v]:
if not visited[x]:
visited[x] = 1
distance[x] = distance[v] + 1
q.append(x)
return distance.count(max(distance))