|
์ฝ๋ ์์ฑํ๊ธฐ
from collections import deque
def solution(n, edge):
graph = {i:[] for i in range(1, n+1)}
global visited
visited = [0 for i in range(n+1)]
for a,b in edge:
graph[a].append(b)
graph[b].append(a)
result = bfs(graph)
return result.count(max(result))
def bfs(graph):
q = deque()
q.append((1,0))
while q:
index, count = q.popleft()
# print(current)
if not visited[index]:
count += 1
visited[index] = count
for num in graph[index]:
q.append((num,count))
return visited
n = 6
edge = [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]]
print(solution(n, edge))
Java
๋ณต์ฌ
|
์ฝ๋ ์ค๋ช
ํ๊ธฐ
โข
bfs๋ก ์ข์ฐ๋ถํฐ ์ฐจ๋ก๋ก ๋
ธ๋ ํ์
โข
index๋ฟ๋ง ์๋๋ผ count๋ฅผ ์ด์ ์ ๋
ธ๋์ ๋นํด ํ๋์ฉ ์ฆ๊ฐ์์ผ์ผํ๊ธฐ๋๋ฌธ์ q์ index์ ํจ๊ป ํ์ฌ์ count๋ฅผ ๋ฃ์ด์ค
โข
์ด ๋ฌธ์ ๋ dfs๋ก ํ๊ฒ๋๋ฉด ์ค๋ณตํ์์ด ๋๋ ๋
ธ๋๊ฐ ์๊ธธ ์ ์์ผ๋ฏ๋ก bfs๋ก ์ข์ฐํ์์ผ๋ก ํด์ฃผ๋๊ฒ์ด ์ข์