Skip to content

Commit

Permalink
#21 : 2606_바이러스_DFS
Browse files Browse the repository at this point in the history
  • Loading branch information
devCharlotte authored May 17, 2023
1 parent 0d64782 commit ffdac8d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions 이티준희/2606_바이러스_DFS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#2023-05-11-Week5-과제
#2606_바이러스

'''
입력
7 총 컴퓨터 수
6 컴퓨터 간 연결된 선 개수
1 2 1번컴퓨터와 2번컴퓨터 연결
2 3
...
4 7
'''

#dfs
import sys

vertex = int(sys.stdin.readline()) # 컴퓨터 개수
edge = int(sys.stdin.readline()) # 연결선 개수

graph = [[] for _ in range(vertex + 1)] # 그래프
visited = [0] * (vertex + 1) # 1번 인덱스부터 사용

for i in range(edge): # 그래프 생성
a, b = map(int, sys.stdin.readline().split())
graph[a].append(b) # a 에 b 연결
graph[b].append(a) # b 에 a 연결


def dfs(node):
visited[node] = 1
for i in graph[node]:
if visited[i] == 0:
dfs(i)


dfs(1) # 1번 컴퓨터부터 시작
print(sum(visited) - 1)

0 comments on commit ffdac8d

Please sign in to comment.