반응형
https://www.acmicpc.net/problem/2606
2606번: 바이러스
첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수가 주어
www.acmicpc.net
import collections
n = int(input())
link = int(input())
table = collections.defaultdict(list)
for i in range(link):
a, b = map(int, input().split())
table[a].append(b)
table[b].append(a)
discovered=[]
def dfs(computer):
for other in table[computer]:
if other not in discovered:
discovered.append(other)
dfs(other)
dfs(1)
print(len(discovered)-1)
DFS로 쉽게 풀 수 있는 문제!!
댓글