Skip to content

Commit

Permalink
solve(programmers): LV3_가장 먼 노드_py
Browse files Browse the repository at this point in the history
# id: 문제 id를 숫자로 작성
# categories : 해당 문제의 유형을 ,로 구분하여 작성
# tags : 해당 문제의 태그를 ,로 구분하여 작성
# time : 해당 문제 풀이에 걸린 시간을 분단위 숫자로 작성
# try : 해당 문제에 몇번의 시도를 했는지 숫자로 작성
# help: 해당 문제에 외부의 도움을 받았는지 true/false로 작성
# url : 해당 문제의 url을 작성
id: 49189
categories: [BFS]
tags: []
time: 15
try: 1
help: false
url: https://school.programmers.co.kr/learn/courses/30/lessons/49189
  • Loading branch information
gogumaC committed Aug 11, 2024
1 parent 0b35087 commit ccd72a9
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/programmers/level_1/BFS/LV3_가장_먼_노드.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from collections import deque

def bfs(graph,start,n):
q=deque()
q.append((start,0))
visited=[0 for _ in range(n+1)]
visited[start]=True
length=[0 for _ in range(n+1)]

while q:
current,depth=q.popleft()
for n in graph[current]:
if visited[n]:
continue
length[n]=depth+1
visited[n]=True
q.append((n,depth+1))

length.sort(reverse=True)

max_l=length[0]
count=0
for l in length:
if l==max_l:
count+=1
else:
break
return count


def solution(n, edge):

graph={}
for (u,v) in edge:
if u not in graph:
graph[u]=[]
if v not in graph:
graph[v]=[]
graph[u].append(v)
graph[v].append(u)

answer = bfs(graph,1,n)
return answer

0 comments on commit ccd72a9

Please sign in to comment.