Skip to content

Commit 0d64782

Browse files
authored
#21 : 2606_바이러스_BFS
1 parent 79f8d37 commit 0d64782

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'''
1414

1515
#bfs 너비 우선 탐색
16+
'''
1617
import sys
1718
from collections import deque
1819
vertex = int(sys.stdin.readline()) # 컴퓨터 개수
@@ -35,3 +36,39 @@
3536
queue.append(i)
3637
visited[i]=1
3738
print(sum(visited)-1)
39+
'''
40+
41+
import sys
42+
from collections import deque
43+
44+
vertex = int(sys.stdin.readline()) # 컴퓨터 개수
45+
edge = int(sys.stdin.readline()) # 연결선 개수
46+
47+
graph = [[] for i in range(vertex + 1)] # 그래프
48+
visited = [0] * (vertex + 1) # 1번 인덱스부터 사용
49+
50+
for i in range(edge): # 그래프 생성
51+
a, b = map(int, sys.stdin.readline().split())
52+
graph[a].append(b) # a 에 b 연결
53+
graph[b].append(a) # b 에 a 연결
54+
55+
56+
def bfs(start):
57+
visited[start] = 1
58+
queue = deque([start])
59+
60+
while queue:
61+
node = queue.popleft()
62+
for i in graph[node]:
63+
if visited[i] == 0:
64+
queue.append(i)
65+
visited[i] = 1
66+
67+
bfs(1) # 1번 컴퓨터부터 시작
68+
print(sum(visited) - 1)
69+
70+
71+
72+
73+
74+

0 commit comments

Comments
 (0)