[python 파이썬] 백준 2606번 바이러스
2021. 2. 25. 01:28ㆍAlgorithm/BOJ
반응형
이 문제는 대표적인 BFS 문제이다. 앞서 푼 1260번에서 사용한 BFS함수를 응용해서 풀었더니 쉽게 풀렸다!
n = int(input())
m = int(input())
matrix=[[0]*(n+1) for i in range(n+1)]
for i in range(m):
a,b = map(int,input().split())
matrix[a][b]=matrix[b][a]=1
visit_list=[1]*(n+1)
def bfs(v):
count=0
queue=[v]
visit_list[v]=0
while queue:
v=queue.pop(0)
count+=1
# print(v,end=' ') #
for i in range(1,n+1):
if(visit_list[i]==1 and matrix[v][i]==1):
queue.append(i)
visit_list[i]=0
return count-1
print(bfs(1))
참고
반응형
'Algorithm > BOJ' 카테고리의 다른 글
[python 파이썬] 백준 11723번 집합 (0) | 2021.03.02 |
---|---|
[python 파이썬] 백준 2667번 단지번호붙이기 (0) | 2021.02.25 |
[python 파이썬] 백준 1260번 DFS와 BFS (0) | 2021.02.23 |
[python 파이썬] 백준 2178번 미로탐색 (0) | 2021.02.23 |
[python 파이썬] 백준 2003번 수들의 합2 (0) | 2021.02.15 |