yha97 2022. 10. 19. 12:08

날짜 : 2022. 10. 19

사용 언어 : python

 

문제

 

 

코드

import sys

cnt = 0
def dfs(node):
    global cnt
    for i in pc[node]:
        if not infested[i]: # 감염되지 않은 경우
            infested[i] = True # 감염
            cnt += 1 # 개수 증가
            dfs(i) # 숙주
    return

n = int(sys.stdin.readline())
c = int(sys.stdin.readline()) # 서로 연결된 컴퓨터 (conntected)
pc = [[]for i in range(n+1)]
infested = [False] * (n+1) # 감염여부 (infested)

for _ in range(c):
    a, b = map(int, sys.stdin.readline().split())
    pc[a].append(b)
    pc[b].append(a)

for i in pc:
    i.sort()

infested[1] = True

dfs(1)

print(cnt)
#print(infested)

 

 

알게된 점

- 어제 구름 챌린지에서 고배를 마시고 오늘 DFS문제를 풀었다.

- 기본적인 DFS문제였고 기본문제는 어느정도 감이 잡힌 듯(?) 보인다.

 

 

참고 사이트