-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0cb552
commit 79f8d37
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#2023-05-11-Week5-과제 | ||
#2606_바이러스 | ||
|
||
''' | ||
입력 | ||
7 총 컴퓨터 수 | ||
6 컴퓨터 간 연결된 선 개수 | ||
1 2 1번컴퓨터와 2번컴퓨터 연결 | ||
2 3 | ||
... | ||
4 7 | ||
''' | ||
|
||
#bfs 너비 우선 탐색 | ||
import sys | ||
from collections import deque | ||
vertex = int(sys.stdin.readline()) # 컴퓨터 개수 | ||
edge = int(sys.stdin.readline()) # 연결선 개수 | ||
|
||
graph = [[NULL] for i in range(vertex+1)] #그래프 | ||
visited = [0]*(vertex+1) #1번 인덱스부터 사용 | ||
|
||
for i in range(edge): # 그래프 생성 | ||
a,b = map(int,stdin.split()) | ||
graph[a]+=[b] # a 에 b 연결 | ||
graph[b]+=[a] # b 에 a 연결 | ||
|
||
visited[1] = 1 # 1번 컴퓨터부터 시작이니 방문 표시 | ||
queue = deque([1]) | ||
while queue : | ||
result = queue.popleft() | ||
for i in graph[result]: | ||
if visited[i]==0: | ||
queue.append(i) | ||
visited[i]=1 | ||
print(sum(visited)-1) |