Created
October 30, 2019 13:14
-
-
Save inspirit941/8e378fad7b0280c0e573de94fe099cf4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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