Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save inspirit941/8e378fad7b0280c0e573de94fe099cf4 to your computer and use it in GitHub Desktop.
Save inspirit941/8e378fad7b0280c0e573de94fe099cf4 to your computer and use it in GitHub Desktop.
from collections import defaultdict, deque
def bfs(start, tables, visited):
queue = deque()
queue.append((start, 0))
visited.add(start)
# 가장 먼 노드의 개수를 세는 dictionary
numbers = defaultdict(lambda:0)
while queue:
node, cnt = queue.popleft()
visited.add(node)
for neighbor in tables[node]:
if neighbor not in visited:
visited.add(neighbor)
numbers[cnt+1] += 1
queue.append((neighbor, cnt+1))
# 가장 먼 거리의 노드 개수를 반환
return numbers[cnt]
def solution(n, edge):
tables = defaultdict(set)
for a, b in edge:
tables[a].add(b)
tables[b].add(a)
visited = set()
return bfs(1, tables, visited)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment