본문 바로가기
백준

바이러스 - 2606번

by 청원뿔세포 2022. 9. 28.

DFS 깊이우선탐색으로 문제를 해결하였다. 

방문했던곳은 True로 표시를 하면서 진행하기 때문에 DFS가 끝나고 나면 True가 몇개인지 세주면 된다.

import sys

l = int(sys.stdin.readline())
c = int(sys.stdin.readline())
graph = [[] for i in range(l+1)]
check = [False] * (l+1)
# print(graph)
for _ in range(c):
    a,b = map(int,sys.stdin.readline().split())
    graph[a].append(b)
    graph[b].append(a)
for i in graph:
    i.sort()

def DFS(start):
    check[start] = True
    for i in graph[start]:
        if not check[i]:
            DFS(i)
DFS(1)
print(check.count(True)-1)

'백준' 카테고리의 다른 글

RGB거리 2 - 17404번  (0) 2022.09.29
RGB거리 - 1149번  (0) 2022.09.29
골드바흐의 추측 - 9020번  (0) 2022.09.27
알고리즘 수업 - 깊이 우선 탐색 2 - 24480번  (0) 2022.09.26
알고리즘 수업 - 깊이 우선 탐색 1 - 24479번  (0) 2022.09.25

댓글