-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 5-ljedd2-02
- Loading branch information
Showing
2 changed files
with
23 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from collections import deque | ||
|
||
def solution(n, edge): | ||
graph = [[] for _ in range(n + 1)] | ||
visited = [0] * (n + 1) | ||
|
||
# 인접 리스트 방식의 그래프 구현 | ||
for a, b in edge: | ||
graph[a].append(b) | ||
graph[b].append(a) | ||
|
||
queue = deque([(1)]) | ||
visited[1] = 1 | ||
|
||
while queue: | ||
x = queue.popleft() | ||
for nx in graph[x]: | ||
if not visited[nx]: # 방문한 적이 없는 경우 | ||
visited[nx] = visited[x] + 1 # 거리 계산 | ||
queue.append(nx) | ||
|
||
max_value = max(visited) | ||
return visited.count(max_value) # 최댓값을 가지는 요소의 개수 |